bookmark-manager-r4/RyzStudio/Windows/Forms/ThreadControl.cs

787 lines
21 KiB
C#
Raw Normal View History

2021-09-07 11:32:24 +00:00
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 TreeNode Add(TreeView parentControl, TreeNodeCollection control, string key, string text, int imageIndex, int selectedImageIndex)
{
TreeNode rv = null;
if (parentControl.InvokeRequired)
{
parentControl.Invoke(new MethodInvoker(() => {
rv = control.Add(key, text, imageIndex, selectedImageIndex);
}));
}
else
{
rv = control.Add(key, text, imageIndex, selectedImageIndex);
}
return rv;
}
2021-09-07 16:21:28 +00:00
public static TreeNode Add(TreeNode control, string key, string text, int imageIndex, int selectedImageIndex)
{
TreeNode rv = null;
if (control.TreeView.InvokeRequired)
{
control.TreeView.Invoke(new MethodInvoker(() => {
rv = control.Nodes.Add(key, text, imageIndex, selectedImageIndex);
}));
}
else
{
rv = control.Nodes.Add(key, text, imageIndex, selectedImageIndex);
}
return rv;
}
public static TreeNode Add(TreeView control, string key, string text, int imageIndex, int selectedImageIndex)
{
TreeNode rv = null;
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
rv = control.Nodes.Add(key, text, imageIndex, selectedImageIndex);
}));
}
else
{
rv = control.Nodes.Add(key, text, imageIndex, selectedImageIndex);
}
return rv;
}
2021-09-30 22:09:55 +00:00
public static void Add(TreeView parentControl, ImageList control, Image value)
{
if (parentControl.InvokeRequired)
{
parentControl.Invoke(new MethodInvoker(() => {
control.Images.Add(value);
}));
}
else
{
control.Images.Add(value);
}
}
2021-09-07 16:21:28 +00:00
public static void Add(Control parentControl, ImageList control, string key, Image value)
{
if (parentControl.InvokeRequired)
{
parentControl.Invoke(new MethodInvoker(() => {
control.Images.Add(key, value);
}));
}
else
{
control.Images.Add(key, value);
}
}
2021-09-07 11:32:24 +00:00
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();
}
}
2021-09-30 22:09:55 +00:00
public static void Clear(TreeView parentControl, ImageList control)
{
if (parentControl.InvokeRequired)
{
parentControl.Invoke(new MethodInvoker(() => {
control.Images.Clear();
}));
}
else
{
control.Images.Clear();
}
}
2021-09-07 11:32:24 +00:00
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 void Expand(TreeNode control)
{
if (control.TreeView.InvokeRequired)
{
control.TreeView.Invoke(new MethodInvoker(() => {
control.Expand();
}));
}
else
{
control.Expand();
}
}
public static List<T> FindChildControl<T>(Control control) where T : Control
{
List<T> rs = new List<T>();
foreach (Control item in control.Controls)
{
var ctr = item as T;
if (ctr == null)
{
rs.AddRange(FindChildControl<T>(item));
}
else
{
rs.Add(ctr);
}
}
return rs;
}
2021-09-17 15:37:01 +00:00
public static string GetSelectedValue(ListBox sender)
2021-09-07 11:32:24 +00:00
{
string rv = string.Empty;
2021-09-17 15:37:01 +00:00
if (sender.InvokeRequired)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
sender.Invoke(new MethodInvoker(() => {
rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString();
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString();
2021-09-07 11:32:24 +00:00
}
return rv;
}
2021-09-17 15:37:01 +00:00
public static string GetText(Control control, bool doTrim = true)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
string rv = string.Empty;
if (control.InvokeRequired)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
control.Invoke(new MethodInvoker(() => {
rv = (doTrim ? control.Text?.Trim() : control.Text);
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
rv = (doTrim ? control.Text?.Trim() : control.Text);
2021-09-07 11:32:24 +00:00
}
2021-09-17 15:37:01 +00:00
return rv;
2021-09-07 11:32:24 +00:00
}
2021-09-17 15:37:01 +00:00
public static int GetValue(NumericUpDown sender)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
int rv = 0;
if (sender.InvokeRequired)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
sender.Invoke(new MethodInvoker(() => {
rv = (int)sender.Value;
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
rv = (int)sender.Value;
2021-09-07 11:32:24 +00:00
}
2021-09-17 15:37:01 +00:00
return rv;
2021-09-07 11:32:24 +00:00
}
2021-09-17 15:37:01 +00:00
public static bool IsChild(TreeNode dragNode, TreeNode dropNode)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
TreeNode tn = dropNode;
while (true)
2021-09-07 16:21:28 +00:00
{
2021-09-17 15:37:01 +00:00
if (tn.Parent == null)
{
break;
}
if (tn.Equals(dragNode))
{
return true;
}
tn = tn.Parent;
2021-09-07 16:21:28 +00:00
}
2021-09-17 15:37:01 +00:00
return false;
}
public static void SetChecked(ToolStripMenuItem control, bool value)
{
2021-09-07 11:32:24 +00:00
if (control.GetCurrentParent().InvokeRequired)
{
control.GetCurrentParent().Invoke(new MethodInvoker(() =>
{
2021-09-17 15:37:01 +00:00
control.Checked = value;
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
control.Checked = value;
2021-09-07 11:32:24 +00:00
}
}
2021-09-17 15:37:01 +00:00
public static void SetClientHeight(Control control, int value)
2021-09-07 11:32:24 +00:00
{
if (control.InvokeRequired)
{
2021-09-17 15:37:01 +00:00
control.Invoke(new MethodInvoker(() => {
control.ClientSize = new Size(control.ClientSize.Width, value);
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
control.ClientSize = new Size(control.ClientSize.Width, value);
2021-09-07 11:32:24 +00:00
}
}
2021-09-17 15:37:01 +00:00
public static void SetClientSize(Control control, int width, int height) => SetClientSize(control, new Size(width, height));
2021-09-07 11:32:24 +00:00
2021-09-17 15:37:01 +00:00
public static void SetClientSize(Control control, Size value)
{
if (control.InvokeRequired)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
control.Invoke(new MethodInvoker(() => {
control.ClientSize = value;
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
control.ClientSize = value;
2021-09-07 11:32:24 +00:00
}
}
2021-09-17 15:37:01 +00:00
public static void SetClientWidth(Control control, int value)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
if (control.InvokeRequired)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
control.Invoke(new MethodInvoker(() => {
control.ClientSize = new Size(value, control.ClientSize.Height);
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
control.ClientSize = new Size(value, control.ClientSize.Height);
2021-09-07 11:32:24 +00:00
}
}
2021-09-17 15:37:01 +00:00
public static void SetEnable(Control control, bool value)
2021-09-07 11:32:24 +00:00
{
if (control.InvokeRequired)
{
2021-09-17 15:37:01 +00:00
control.Invoke(new MethodInvoker(() =>
{
control.Enabled = value;
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
control.Enabled = value;
2021-09-07 11:32:24 +00:00
}
}
2021-09-17 15:37:01 +00:00
public static void SetEnable(ToolStripMenuItem control, bool value)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
if (control.GetCurrentParent() == null)
2021-09-07 11:32:24 +00:00
{
2021-09-17 15:37:01 +00:00
control.Enabled = value;
return;
}
if (control.GetCurrentParent().InvokeRequired)
{
control.GetCurrentParent().Invoke(new MethodInvoker(() =>
{
control.Enabled = value;
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
control.Enabled = value;
2021-09-07 11:32:24 +00:00
}
}
2021-09-17 15:37:01 +00:00
public static void SetFocus(Control control)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() =>
{
control.Focus();
}));
}
else
{
control.Focus();
}
}
2021-09-07 11:32:24 +00:00
2021-09-17 15:37:01 +00:00
public static void SetHeight(Control control, int value)
2021-09-07 11:32:24 +00:00
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
2021-09-17 15:37:01 +00:00
control.Height = value;
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
control.Height = value;
2021-09-07 11:32:24 +00:00
}
}
2021-09-17 15:37:01 +00:00
public static void SetLocation(Control control, Point value)
2021-09-07 11:32:24 +00:00
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
2021-09-17 15:37:01 +00:00
control.Location = value;
2021-09-07 11:32:24 +00:00
}));
}
else
{
2021-09-17 15:37:01 +00:00
control.Location = value;
2021-09-07 11:32:24 +00:00
}
}
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 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;
}
}
2021-09-30 21:26:17 +00:00
public static void SetValue(ComboBox control, int value)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
control.SelectedIndex = value;
}));
}
else
{
control.SelectedIndex = value;
}
}
2021-09-07 11:32:24 +00:00
public static void SetValue(PictureBox control, Image value)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() =>
{
control.Image = value;
}));
}
else
{
control.Image = value;
}
}
2021-09-30 21:26:17 +00:00
public static 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 static int GetValue(ComboBox sender)
{
int rv = -1;
if (sender.InvokeRequired)
{
sender.Invoke(new MethodInvoker(() => {
rv = sender.SelectedIndex;
}));
}
else
{
rv = sender.SelectedIndex;
}
return rv;
}
2021-09-07 11:32:24 +00:00
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;
}
}
}
}