From f82dfbc308a07bb623bd9f8719b776e7774fe493 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 23 Aug 2020 14:42:33 +0100 Subject: [PATCH] Changed: thread helper --- BookmarkEditForm.cs | 8 +- OptionForm.cs | 44 +- RyzStudio/Windows/Forms/ThreadControl.cs | 570 ++++++++++++++++++ RyzStudio/Windows/ThemedForms/Form.cs | 105 +--- .../Windows/ThemedForms/ProgressBarInner.cs | 16 +- bomg.csproj | 1 - 6 files changed, 582 insertions(+), 162 deletions(-) create mode 100644 RyzStudio/Windows/Forms/ThreadControl.cs diff --git a/BookmarkEditForm.cs b/BookmarkEditForm.cs index 4790f05..35e91d4 100644 --- a/BookmarkEditForm.cs +++ b/BookmarkEditForm.cs @@ -45,10 +45,10 @@ namespace bzit.bomg bool rv = itemModel.Update(); if (rv) { - ThreadHelper.SetText(textBox1, itemModel.SiteName); - ThreadHelper.SetText(textBox2, itemModel.SiteAddress); - ThreadHelper.SetText(memoBox1, itemModel.SiteDescription); - ThreadHelper.SetImage(pictureBox2, itemModel.RetrieveFavicon()); + ThreadControl.SetText(textBox1, itemModel.SiteName); + ThreadControl.SetText(textBox2, itemModel.SiteAddress); + ThreadControl.SetText(memoBox1, itemModel.SiteDescription); + ThreadControl.SetImage(pictureBox2, itemModel.RetrieveFavicon()); } } diff --git a/OptionForm.cs b/OptionForm.cs index 4c4e659..c7a3705 100644 --- a/OptionForm.cs +++ b/OptionForm.cs @@ -43,18 +43,7 @@ namespace bzit.bomg List nodeList = parentForm.treeView1.GetBookmarkNodeList(); - if (progressBar1.InvokeRequired) - { - progressBar1.Invoke(new MethodInvoker(() => { - progressBar1.Maximum = nodeList.Count; - progressBar1.Value = 0; - })); - } - else - { - progressBar1.Maximum = nodeList.Count; - progressBar1.Value = 0; - } + ThreadControl.SetValue(progressBar1, 0, nodeList.Count); for (int i=0; i { - progressBar1.Value = (i + 1); - })); - } - else - { - progressBar1.Value = (i + 1); - } + ThreadControl.SetValue(progressBar1, (i + 1)); BookmarkItemViewModel viewModel = (BookmarkItemViewModel)nodeList[i].Tag; BookmarkItemModel model = viewModel.ToModel(); @@ -97,7 +77,7 @@ namespace bzit.bomg { this.IsBusy = false; - setLabelText("&Update"); + ThreadControl.SetText(button2, "&Update"); } protected override void OnClosing(CancelEventArgs e) @@ -123,7 +103,7 @@ namespace bzit.bomg { if (this.IsBusy) { - setLabelText("&Cancelling..."); + ThreadControl.SetText(button2, "&Cancelling..."); threadWorker.CancelAsync(); } @@ -131,7 +111,7 @@ namespace bzit.bomg { this.IsBusy = true; - setLabelText("&Updating..."); + ThreadControl.SetText(button2, "&Updating..."); threadWorker.RunWorkerAsync(); } @@ -147,19 +127,5 @@ namespace bzit.bomg this.Close(); } - protected void setLabelText(string text) - { - if (button1.InvokeRequired) - { - button1.Invoke(new MethodInvoker(() => { - button1.LabelText = text; - })); - } - else - { - button1.LabelText = text; - } - } - } } \ No newline at end of file diff --git a/RyzStudio/Windows/Forms/ThreadControl.cs b/RyzStudio/Windows/Forms/ThreadControl.cs new file mode 100644 index 0000000..b63586b --- /dev/null +++ b/RyzStudio/Windows/Forms/ThreadControl.cs @@ -0,0 +1,570 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RyzStudio.Windows.Forms +{ + public class ThreadControl + { + public static void Add(Control control, Control value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => + { + control.Controls.Add(value); + })); + } + else + { + control.Controls.Add(value); + } + } + + public static void Add(ListBox control, string value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Items.Add(value); + })); + } + else + { + control.Items.Add(value); + } + } + + //public static void Add(FlowLayoutPanel control, Control value) + //{ + // if (control.InvokeRequired) + // { + // control.Invoke(new MethodInvoker(() => { + // control.Controls.Add(value); + // })); + // } + // else + // { + // control.Controls.Add(value); + // } + //} + + public static int Add(TreeView control, TreeNode value) + { + int n = -1; + + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + n = control.Nodes.Add(value); + })); + } + else + { + n = control.Nodes.Add(value); + } + + return n; + } + + public static int Add(TreeNode control, TreeNode value) + { + int n = -1; + + if (control.TreeView.InvokeRequired) + { + control.TreeView.Invoke(new MethodInvoker(() => { + n = control.Nodes.Add(value); + })); + } + else + { + n = control.Nodes.Add(value); + } + + return n; + } + + public static int Add(DataGridView control, object[] value) + { + int n = -1; + + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + n = control.Rows.Add(value); + })); + } + else + { + n = control.Rows.Add(value); + } + + return n; + } + + public static int Add(DataGridView control, object[] value, object tag) + { + int n = -1; + + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + n = control.Rows.Add(value); + control.Rows[n].Tag = tag; + })); + } + else + { + n = control.Rows.Add(value); + control.Rows[n].Tag = tag; + } + + return n; + } + + public static TreeNode Add(TreeView control, string key, string text) + { + TreeNode rv = null; + + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + rv = control.Nodes.Add(key, text); + })); + } + else + { + rv = control.Nodes.Add(key, text); + } + + return rv; + } + + public static TreeNode Add(TreeNode control, string key, string text) + { + TreeNode rv = null; + + if (control.TreeView.InvokeRequired) + { + control.TreeView.Invoke(new MethodInvoker(() => { + rv = control.Nodes.Add(key, text); + })); + } + else + { + rv = control.Nodes.Add(key, text); + } + + return rv; + } + + public static void AddLine(RichTextBox control, string text) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => + { + control.Text += text + Environment.NewLine; + })); + } + else + { + control.Text += text + Environment.NewLine; + } + } + + public static void Clear(ListBox control) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Items.Clear(); + })); + } + else + { + control.Items.Clear(); + } + } + + public static void Clear(FlowLayoutPanel control) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Controls.Clear(); + })); + } + else + { + control.Controls.Clear(); + } + } + + public static void Clear(TreeView control) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Nodes.Clear(); + })); + } + else + { + control.Nodes.Clear(); + } + } + + public static void Clear(Label control) => SetText(control, string.Empty); + + public static void Clear(PictureBox control) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => + { + control.Image = null; + })); + } + else + { + control.Image = null; + } + } + + public static void Clear(DataGridView control) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => + { + control.Rows.Clear(); + })); + } + else + { + control.Rows.Clear(); + } + } + + public static List FindChildControl(Control control) where T : Control + { + List rs = new List(); + + foreach (Control item in control.Controls) + { + var ctr = item as T; + if (ctr == null) + { + rs.AddRange(FindChildControl(item)); + } + else + { + rs.Add(ctr); + } + } + + return rs; + } + + public static string GetText(Control control, bool doTrim = true) + { + string rv = string.Empty; + + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + rv = (doTrim ? control.Text?.Trim() : control.Text); + })); + } + else + { + rv = (doTrim ? control.Text?.Trim() : control.Text); + } + + return rv; + } + + public static void SetEnable(Control control, bool value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => + { + control.Enabled = value; + })); + } + else + { + control.Enabled = value; + } + } + + public static int GetValue(NumericUpDown sender) + { + int rv = 0; + + if (sender.InvokeRequired) + { + sender.Invoke(new MethodInvoker(() => { + rv = (int)sender.Value; + })); + } + else + { + rv = (int)sender.Value; + } + + return rv; + } + + public static string GetSelectedValue(ListBox sender) + { + string rv = string.Empty; + + if (sender.InvokeRequired) + { + sender.Invoke(new MethodInvoker(() => { + rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString(); + })); + } + else + { + rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString(); + } + + return rv; + } + + public static void SetHeight(Control control, int value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Height = value; + })); + } + else + { + control.Height = value; + } + } + + public static void SetImage(PictureBox control, Image image) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Image = image; + })); + } + else + { + control.Image = image; + } + } + + public static void SetSize(Control control, int width, int height) => SetSize(control, new Size(width, height)); + + public static void SetSize(Control control, Size value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Size = value; + })); + } + else + { + control.Size = value; + } + } + + public static void SetText(Control control, string text) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => + { + control.Text = text; + })); + } + else + { + control.Text = text; + } + } + + public static void SetText(ThemedForms.MemoBox control, string text) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Text = text; + })); + } + else + { + control.Text = text; + } + } + + public static void SetText(ThemedForms.Button control, string text) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.LabelText = text; + })); + } + else + { + control.LabelText = text; + } + } + + public static void SetTopMost(Form control, bool value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.TopMost = value; + })); + } + else + { + control.TopMost = value; + } + } + + public static void SetValue(Control control, string value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Text = value; + })); + } + else + { + control.Text = value; + } + } + + public static void SetValue(PictureBox control, Image value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => + { + control.Image = value; + })); + } + else + { + control.Image = value; + } + } + + public static void SetValue(ThemedForms.ProgressBar control, int value, int maximum) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Maximum = maximum; + control.Value = value; + })); + } + else + { + control.Maximum = maximum; + control.Value = value; + } + } + + public static void SetValue(ThemedForms.ProgressBar control, int value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Value = value; + })); + } + else + { + control.Value = value; + } + } + + public static void SetVisible(Control control, bool value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Visible = value; + })); + } + else + { + control.Visible = value; + } + } + + public static void SetVisible(Form control, bool value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + if (value) + { + control.ShowInTaskbar = value; + control.Opacity = 100; + control.Visible = value; + } + else + { + control.Visible = value; + control.ShowInTaskbar = value; + control.Opacity = 0; + } + })); + } + else + { + if (value) + { + control.ShowInTaskbar = value; + control.Opacity = 100; + control.Visible = value; + } + else + { + control.Visible = value; + control.ShowInTaskbar = value; + control.Opacity = 0; + } + } + } + + public static void SetWidth(Control control, int value) + { + if (control.InvokeRequired) + { + control.Invoke(new MethodInvoker(() => { + control.Width = value; + })); + } + else + { + control.Width = value; + } + } + + + } +} \ No newline at end of file diff --git a/RyzStudio/Windows/ThemedForms/Form.cs b/RyzStudio/Windows/ThemedForms/Form.cs index 00dcbbf..f4ed713 100644 --- a/RyzStudio/Windows/ThemedForms/Form.cs +++ b/RyzStudio/Windows/ThemedForms/Form.cs @@ -76,111 +76,8 @@ } } - #region public properties - - #endregion - - #region public properties (encapsulation) - [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] - public new Color BackColor { get { return base.BackColor; } set { base.BackColor = value; } } + public new Color BackColor { get => base.BackColor; set => base.BackColor = value; } - #endregion - - public void SetValue(Label sender, string value) - { - if (sender.InvokeRequired) - { - sender.Invoke(new MethodInvoker(() => { sender.Text = value; })); - } - else - { - sender.Text = value; - } - } - - public void SetValue(GroupBox sender, string value) - { - if (sender.InvokeRequired) - { - sender.Invoke(new MethodInvoker(() => { sender.Text = value; })); - } - else - { - sender.Text = value; - } - } - - public void AddValue(ListBox sender, string value) - { - if (sender.InvokeRequired) - { - sender.Invoke(new MethodInvoker(() => { sender.Items.Add(value); })); - } - else - { - sender.Items.Add(value); - } - } - - public void ClearValues(ListBox sender) - { - if (sender.InvokeRequired) - { - sender.Invoke(new MethodInvoker(() => { sender.Items.Clear(); })); - } - else - { - sender.Items.Clear(); - } - } - - public string GetValue(ListBox sender) - { - string rv = string.Empty; - - if (sender.InvokeRequired) - { - sender.Invoke(new MethodInvoker(() => { rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString(); })); - } - else - { - rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString(); - } - - return rv; - } - - public string GetValue(TextBox sender) - { - string rv = string.Empty; - - if (sender.InvokeRequired) - { - sender.Invoke(new MethodInvoker(() => { rv = sender.Text.Trim(); })); - } - else - { - rv = sender.Text.Trim(); - } - - return rv; - } - - public int GetValue(NumericUpDown sender) - { - int rv = 0; - - if (sender.InvokeRequired) - { - sender.Invoke(new MethodInvoker(() => { rv = (int)sender.Value; })); - } - else - { - rv = (int)sender.Value; - } - - return rv; - } } } \ No newline at end of file diff --git a/RyzStudio/Windows/ThemedForms/ProgressBarInner.cs b/RyzStudio/Windows/ThemedForms/ProgressBarInner.cs index acf6004..974a7d2 100644 --- a/RyzStudio/Windows/ThemedForms/ProgressBarInner.cs +++ b/RyzStudio/Windows/ThemedForms/ProgressBarInner.cs @@ -105,20 +105,7 @@ namespace RyzStudio.Windows.ThemedForms } } - protected void updateText() - { - if (label3.InvokeRequired) - { - label3.Invoke(new MethodInvoker(() => - { - label3.Text = string.Format("{0}/{1}", this.Value.ToString(), this.Maximum.ToString()); - })); - } - else - { - label3.Text = string.Format("{0}/{1}", this.Value.ToString(), this.Maximum.ToString()); - } - } + protected void updateText() => RyzStudio.Windows.Forms.ThreadControl.SetText(label3, string.Format("{0}/{1}", this.Value.ToString(), this.Maximum.ToString())); protected void setMinimum(int value) { @@ -193,5 +180,6 @@ namespace RyzStudio.Windows.ThemedForms updateText(); this.Invalidate(); } + } } diff --git a/bomg.csproj b/bomg.csproj index c051be6..cacb1ac 100644 --- a/bomg.csproj +++ b/bomg.csproj @@ -118,7 +118,6 @@ HorizontalSeparator.cs - UserControl