static public TextBoxEx GetTextBoxAndFocus(object sender, bool doFocus = true)
    {
        var control = sender as TextBoxEx;

        if (control is null)
        {
            if (sender is ContextMenuStrip menuContext)
            {
                control = menuContext.SourceControl as TextBoxEx;
            }
            else
            if (sender is ToolStripMenuItem menuItem)
            {
                control = (menuItem.GetCurrentParent() as ContextMenuStrip)?.SourceControl as TextBoxEx;
            }
        }
        if (control is null)
        {
            var form = FormsHelper.GetActiveForm();
            if (form is not null)
            {
                if (form.ActiveControl is TextBoxEx)
                {
                    control = form.ActiveControl as TextBoxEx;
                }
                else
                if (form.ActiveControl is UserControl)
                {
                    control = (form.ActiveControl as UserControl)?.ActiveControl as TextBoxEx;
                }
            }
        }
        if (doFocus && control?.Enabled == true && !control.Focused)
        {
            control.Focus();
        }
        return(control);
    }
Example #2
0
    static public bool Run(ref DateTime lastdone, int interval, bool auto, bool checkAtStartup, bool useGitHub = false)
    {
        if (interval == -1)
        {
            interval = DefaultCheckDaysInterval;
        }
        CleanTemp();
        if (Mutex)
        {
            return(false);
        }
        if (auto && !checkAtStartup)
        {
            return(false);
        }
        if (auto && lastdone.AddDays(interval) >= DateTime.Now)
        {
            return(false);
        }
        var  form        = FormsHelper.GetActiveForm();
        bool formEnabled = form?.Enabled ?? false;
        bool formTopMost = form?.TopMost ?? false;
        bool formHidden  = LoadingForm.Instance.Hidden;

        try
        {
            Mutex = true;
            if (form is not null)
            {
                form.TopMost = true;
                form.TopMost = formTopMost;
                form.Enabled = false;
            }
            LoadingForm.Instance.Hidden = auto;
            LoadingForm.Instance.Initialize(SysTranslations.WebCheckUpdate.GetLang(), 3, 0, false);
            LoadingForm.Instance.Owner = Globals.MainForm;
            LoadingForm.Instance.DoProgress();
            using var client = new WebClientEx();
            var fileInfo = GetVersionAndChecksum(client, useGitHub);
            lastdone = DateTime.Now;
            if (fileInfo.Item1.CompareTo(Assembly.GetExecutingAssembly().GetName().Version) > 0)
            {
                return(GetUserChoice(client, fileInfo, useGitHub));
            }
            else
            if (!auto)
            {
                DisplayManager.ShowInformation(SysTranslations.NoNewVersionAvailable.GetLang());
            }
        }
        catch (UnauthorizedAccessException ex)
        {
            CleanTemp();
            DisplayManager.ShowWarning(SysTranslations.CheckUpdate.GetLang(Globals.AssemblyTitle), ex.Message);
            if (DisplayManager.QueryYesNo(SysTranslations.AskToOpenGitHubPage.GetLang()))
            {
                SystemManager.OpenGitHupRepo();
            }
        }
        catch (IOException ex)
        {
            CleanTemp();
            DisplayManager.ShowWarning(SysTranslations.CheckUpdate.GetLang(Globals.AssemblyTitle), ex.Message);
            if (DisplayManager.QueryYesNo(SysTranslations.AskToOpenGitHubPage.GetLang()))
            {
                SystemManager.OpenGitHupRepo();
            }
        }
        catch (WebException ex)
        {
            // TODO create advanced box to retry-cancel
            CleanTemp();
            string msg = ex.Message;
            if (ex.Status == WebExceptionStatus.Timeout)
            {
                if (auto)
                {
                    if (useGitHub)
                    {
                        return(false);
                    }
                    else
                    {
                        return(Run(ref lastdone, interval, auto, checkAtStartup, true));
                    }
                }
                else
                if (useGitHub)
                {
                    msg += Globals.NL2 + SysTranslations.CheckInternetConnection.GetLang();
                }
                else
                {
                    return(Run(ref lastdone, interval, auto, checkAtStartup, true));
                }
            }
            DisplayManager.ShowWarning(SysTranslations.CheckUpdate.GetLang(Globals.AssemblyTitle), msg);
        }
        catch (Exception ex)
        {
            CleanTemp();
            DisplayManager.ShowWarning(SysTranslations.CheckUpdate.GetLang(Globals.AssemblyTitle), ex.Message);
        }
        finally
        {
            doFinally();
        }
        return(false);

        //
        void doFinally()
        {
            Mutex = false;
            LoadingForm.Instance.Hidden = formHidden;
            LoadingForm.Instance.Hide();
            if (form is not null)
            {
                form.TopMost = true;
                form.TopMost = formTopMost;
                form.Enabled = formEnabled;
            }
        }
    }