private void txtText_TextChanged(object sender, EventArgs e)
 {
     OnTemplateSettingsChanged?.Invoke(this, new TemplateChangeEventArgs
     {
         BackColor = backColor,
         Text      = txtText.Text,
         TextColor = textColor
     });
 }
        private void btnTextColor_Click(object sender, EventArgs e)
        {
            var dialog = new ColorDialog {
                Color = textColor
            };

            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                textColor = dialog.Color;
                ((Button)sender).BackColor = dialog.Color;

                OnTemplateSettingsChanged?.Invoke(this, new TemplateChangeEventArgs
                {
                    BackColor = backColor,
                    Text      = txtText.Text,
                    TextColor = textColor
                });
            }
        }
        private void btnUseOrgTheme_Click(object sender, EventArgs e)
        {
            btnUseOrgTheme.Text    = @"Connecting...";
            btnUseOrgTheme.Enabled = false;
            Cursor = Cursors.WaitCursor;

            var bw = new BackgroundWorker {
                WorkerReportsProgress = true
            };

            bw.DoWork += (s, evt) =>
            {
                var svc = detail.ServiceClient;

                ((BackgroundWorker)s).ReportProgress(50);

                evt.Result = svc.RetrieveMultiple(new QueryExpression
                {
                    EntityName = "theme",
                    ColumnSet  = new ColumnSet("navbarbackgroundcolor"),
                    Criteria   = new FilterExpression
                    {
                        Conditions =
                        {
                            new ConditionExpression("isdefaulttheme", ConditionOperator.Equal, true)
                        }
                    }
                }).Entities.FirstOrDefault();
            };
            bw.RunWorkerCompleted += (s, evt) =>
            {
                btnUseOrgTheme.Text    = @"Use organization theme";
                btnUseOrgTheme.Enabled = true;
                Cursor = Cursors.Default;

                if (evt.Error != null)
                {
                    MessageBox.Show(this,
                                    $@"An error occured when retrieving theme: {evt.Error.Message}",
                                    @"Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                else if (evt.Result is Entity theme)
                {
                    backColor = ColorTranslator.FromHtml(theme.GetAttributeValue <string>("navbarbackgroundcolor"));
                    btnBackColor.BackColor = backColor;
                    textColor = Color.White;
                    btnTextColor.BackColor = textColor;
                    txtText.Text           = txtText.Text.Length == 0 ? detail.ConnectionName : txtText.Text;

                    OnTemplateSettingsChanged?.Invoke(this, new TemplateChangeEventArgs
                    {
                        BackColor = backColor,
                        Text      = txtText.Text,
                        TextColor = textColor
                    });
                }
            };
            bw.ProgressChanged += (s, evt) =>
            {
                if (evt.ProgressPercentage == 50)
                {
                    btnUseOrgTheme.Text = @"Retrieving theme...";
                }
            };
            bw.RunWorkerAsync();
        }