[WinForm]WinForm跨线程UI操作常用控件类大全

图片来自网络/图文无关

前言

在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作。不过会有一个问题:就是涉及到跨线程操作UI元素。
相信才开始接触的人一定会遇上这个问题。

为了解决这个问题,可以通过委托来实现。

我为了后期使用更加方便,就将常用的几个控件(TextBox、Label、CheckBox、RadioButton、ProgressBar)UI操作进行了封装,源码可以在github上查看:GitHub仓库地址
源码在github仓库中的XY.WINUI.ControlEx.InvokeEx文件夹中

测试窗体

使用示例

在测试项目中有使用的方法,这里只做简单介绍。
调用方法在Invokes类中,也可以使用单独的类调用。

例如:在窗体中为tbName的TextBox设置和获取值

//首先,使用命名空间
using XY.WINUI.ControlEx.InvokeEx;

//第一种
//设置TextBox的值
Invokes.SetTextBoxText(tbName,"TextBox文本");
//获取TextBox的值
string value = Invokes.GetTextBoxText(tbName);


//第二种
//设置TextBox的值
TextBoxInvoke.SetTextBoxText(tbName,"TextBox文本");
//获取TextBox的值
string value = TextBoxInvoke.GetTextBoxText(tbName);

GitHub源码

帮助类源码

集合类(Invokes.cs)
Invokes.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace XY.WINUI.ControlEx.InvokeEx
{
    public class Invokes
    {
        #region //Controls

        #region // TextBox
        /// <summary>
        /// 设置文本框文本
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Text"></param>
        public static void SetTextBoxText(TextBox Ctrl, string Text)
        {
            TextBoxInvoke.SetTextBoxText(Ctrl, Text);
        }

        /// <summary>
        /// 获取文本框文本方法
        /// </summary>
        /// <param name="Ctrl"></param>
        public static string GetTextBoxText(TextBox Ctrl)
        {
            return TextBoxInvoke.GetTextBoxText(Ctrl); ;
        }
        #endregion

        #region // Label
        /// <summary>
        /// 设置标签文本
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Text"></param>
        public static void SetLabelText(Label Ctrl, string Text)
        {
            LabelInvoke.SetLabelText(Ctrl, Text);
        }

        /// <summary>
        /// 获取标签文本方法
        /// </summary>
        /// <param name="Ctrl"></param>
        public static string GetLabelText(Label Ctrl)
        {
            return LabelInvoke.GetLabelText(Ctrl); ;
        }
        #endregion 

        #region // RadioButton
        /// <summary>
        /// 获取单选按钮选中状态
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <returns></returns>
        public static bool GetRadioButtonChecked(RadioButton Ctrl)
        {
            return RadioButtonInvoke.GetRadioButtonChecked(Ctrl);
        }

        /// <summary>
        /// 设置单选按钮选中状态
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Checked"></param>
        public static void SetRadioButtonChecked(RadioButton Ctrl, bool Checked)
        {
            RadioButtonInvoke.SetRadioButtonChecked(Ctrl, Checked);
        }
        #endregion

        #region // CheckBox
        /// <summary>
        /// 获取选择按钮选中状态
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <returns></returns>
        public static bool GetCheckBoxChecked(CheckBox Ctrl)
        {
            return CheckBoxInvoke.GetCheckBoxChecked(Ctrl);
        }

        /// <summary>
        /// 设置选择按钮选中状态
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Checked"></param>
        public static void SetCheckBoxChecked(CheckBox Ctrl, bool Checked)
        {
            CheckBoxInvoke.SetCheckBoxChecked(Ctrl, Checked);
        }
        #endregion

        #region // ProgressBar
        /// <summary>
        /// 获取进度条值
        /// </summary>
        /// <param name="Ctrl"></param>
        public static int GetProgressBarValue(ProgressBar Ctrl)
        {
            return ProgressBarInvoke.GetProgressBarValue(Ctrl);
        }
        /// <summary>
        /// 设置进度条值
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        public static void SetProgressBarValue(ProgressBar Ctrl, int Value)
        {
            ProgressBarInvoke.SetProgressBarValue(Ctrl, Value);
        }

        /// <summary>
        /// 获取进度条最小值
        /// </summary>
        /// <param name="Ctrl"></param>
        public static int GetProgressBarMinValue(ProgressBar Ctrl)
        {
            return ProgressBarInvoke.GetProgressBarMinValue(Ctrl);
        }
        /// <summary>
        /// 设置进度条最小值
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        public static void SetProgressBarMinValue(ProgressBar Ctrl, int Value)
        {
            ProgressBarInvoke.SetProgressBarMinValue(Ctrl, Value);
        }

        /// <summary>
        /// 获取进度条最大值
        /// </summary>
        /// <param name="Ctrl"></param>
        public static int GetProgressBarMaxValue(ProgressBar Ctrl)
        {
            return ProgressBarInvoke.GetProgressBarMaxValue(Ctrl);
        }
        /// <summary>
        /// 设置进度条最大值
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        public static void SetProgressBarMaxValue(ProgressBar Ctrl, int Value)
        {
            ProgressBarInvoke.SetProgressBarMaxValue(Ctrl, Value);
        }
        #endregion

        #endregion

        #region // Common

        #region // Visible
        /// <summary>
        /// 获取控件是否可见委托
        /// </summary>
        /// <param name="Ctrl"></param>
        private delegate bool GetControlVisibleDelegate(Control Ctrl);
        /// <summary>
        /// 设置控件是否可见委托
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Visible"></param>
        private delegate void SetControlVisibleDelegate(Control Ctrl, bool Visible);

        /// <summary>
        /// 获取控件是否可见方法
        /// </summary>
        /// <param name="Ctrl"></param>
        private static bool GetControlVisibleFunc(Control Ctrl)
        {
            return Ctrl.Visible;
        }

        /// <summary>
        /// 获取控件是否可见
        /// </summary>
        /// <param name="flag"></param>
        public static bool GetControlVisible(Control Ctrl)
        {
            return (bool)Ctrl.Invoke(new SetControlVisibleDelegate(SetControlVisibleFunc), new object[] { Ctrl });
        }

        /// <summary>
        /// 设置控件是否可见方法
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Visible"></param>
        private static void SetControlVisibleFunc(Control Ctrl, bool Visible)
        {
            Ctrl.Visible = Visible;
            Ctrl.Refresh();
        }

        /// <summary>
        /// 设置控件是否可见
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Visible"></param>
        public static void SetControlVisible(Control Ctrl, bool Visible)
        {
            Ctrl.Invoke(new SetControlVisibleDelegate(SetControlVisibleFunc), new object[] { Ctrl, Visible });
        } 
        #endregion

        #endregion
    }
}

文本框(TextBox)
TextBoxInvoke.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace XY.WINUI.ControlEx.InvokeEx
{
    /// <summary>
    /// TextBoxInvoke
    /// </summary>
    public class TextBoxInvoke
    {
        /// <summary>
        /// 获取文本框文本委托
        /// </summary>
        /// <param name="Ctrl"></param>
        private delegate string GetTextBoxTextDelegate(TextBox Ctrl);
        /// <summary>
        /// 设置文本框文本委托
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Text"></param>
        private delegate void SetTextBoxTextDelegate(TextBox Ctrl, string Text);

        /// <summary>
        /// 获取文本框文本方法
        /// </summary>
        /// <param name="Ctrl"></param>
        private static string GetTextBoxTextFunc(TextBox Ctrl)
        {
            return Ctrl.Text;
        }

        /// <summary>
        /// 获取文本框文本
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <returns></returns>
        public static string GetTextBoxText(TextBox Ctrl)
        {
            return (string)Ctrl.Invoke(new GetTextBoxTextDelegate(GetTextBoxTextFunc), new object[] { Ctrl });
        }

        /// <summary>
        /// 设置文本框文本方法
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Text"></param>
        private static void SetTextBoxTextFunc(TextBox Ctrl, string Text)
        {
            Ctrl.Text = Text;
            Ctrl.Refresh();
        }

        /// <summary>
        /// 设置文本框文本
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Text"></param>
        public static void SetTextBoxText(TextBox Ctrl, string Text)
        {
            Ctrl.Invoke(new SetTextBoxTextDelegate(SetTextBoxTextFunc), new object[] { Ctrl, Text });
        }
    }
}

标签(Label)
LabelInvoke.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace XY.WINUI.ControlEx.InvokeEx
{
    /// <summary>
    /// LabelInvoke
    /// </summary>
    public class LabelInvoke
    {
        /// <summary>
        /// 获取标签文本委托
        /// </summary>
        /// <param name="Ctrl"></param>
        private delegate string GetLabelTextDelegate(Label Ctrl);
        /// <summary>
        /// 设置标签文本委托
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Text"></param>
        private delegate void SetLabelTextDelegate(Label Ctrl, string Text);

        /// <summary>
        /// 获取标签文本方法
        /// </summary>
        /// <param name="Ctrl"></param>
        private static string GetLabelTextFunc(Label Ctrl)
        {
            return Ctrl.Text;
        }

        /// <summary>
        /// 获取标签文本
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <returns></returns>
        public static string GetLabelText(Label Ctrl)
        {
            return (string)Ctrl.Invoke(new GetLabelTextDelegate(GetLabelTextFunc), new object[] { Ctrl });
        }

        /// <summary>
        /// 设置标签文本方法
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Text"></param>
        private static void SetLabelTextFunc(Label Ctrl, string Text)
        {
            Ctrl.Text = Text;
            Ctrl.Refresh();
        }

        /// <summary>
        /// 设置标签文本
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Text"></param>
        public static void SetLabelText(Label Ctrl, string Text)
        {
            Ctrl.Invoke(new SetLabelTextDelegate(SetLabelTextFunc), new object[] { Ctrl, Text });
        }
    }
}

复选框(CheckBox)
CheckBoxInvoke.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace XY.WINUI.ControlEx.InvokeEx
{
    /// <summary>
    /// CheckBoxInvoke
    /// </summary>
    public class CheckBoxInvoke
    {
        /// <summary>
        /// 获取选择按钮选中状态委托
        /// </summary>
        /// <param name="Ctrl"></param>
        private delegate bool GetCheckBoxCheckedDelegate(CheckBox Ctrl);
        /// <summary>
        /// 设置选择按钮选中状态委托
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Checked"></param>
        private delegate void SetCheckBoxCheckedDelegate(CheckBox Ctrl, bool Checked);

        /// <summary>
        /// 获取选择按钮选中状态方法
        /// </summary>
        /// <param name="Ctrl"></param>
        private static bool GetCheckBoxCheckedFunc(CheckBox Ctrl)
        {
            return Ctrl.Checked;
        }

        /// <summary>
        /// 获取选择按钮选中状态
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <returns></returns>
        public static bool GetCheckBoxChecked(CheckBox Ctrl)
        {
            return (bool)Ctrl.Invoke(new GetCheckBoxCheckedDelegate(GetCheckBoxCheckedFunc), new object[] { Ctrl });
        }

        /// <summary>
        /// 设置选择按钮选中状态方法
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Checked"></param>
        private static void SetCheckBoxCheckedFunc(CheckBox Ctrl, bool Checked)
        {
            Ctrl.Checked = Checked;
            Ctrl.Refresh();
        }

        /// <summary>
        /// 设置选择按钮选中状态
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Checked"></param>
        public static void SetCheckBoxChecked(CheckBox Ctrl, bool Checked)
        {
            Ctrl.Invoke(new SetCheckBoxCheckedDelegate(SetCheckBoxCheckedFunc), new object[] { Ctrl, Checked });
        }
    }
}

单选按钮(RadioButton)
RadioButtonInvoke.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace XY.WINUI.ControlEx.InvokeEx
{
    /// <summary>
    /// RadioButtonInvoke
    /// </summary>
    public class RadioButtonInvoke
    {
        /// <summary>
        /// 获取单选按钮选中状态委托
        /// </summary>
        /// <param name="Ctrl"></param>
        private delegate bool GetRadioButtonCheckedDelegate(RadioButton Ctrl);
        /// <summary>
        /// 设置单选按钮选中状态委托
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Checked"></param>
        private delegate void SetRadioButtonCheckedDelegate(RadioButton Ctrl, bool Checked);

        /// <summary>
        /// 获取单选按钮选中状态方法
        /// </summary>
        /// <param name="Ctrl"></param>
        private static bool GetRadioButtonCheckedFunc(RadioButton Ctrl)
        {
            return Ctrl.Checked;
        }

        /// <summary>
        /// 获取单选按钮选中状态
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <returns></returns>
        public static bool GetRadioButtonChecked(RadioButton Ctrl)
        {
            return (bool)Ctrl.Invoke(new GetRadioButtonCheckedDelegate(GetRadioButtonCheckedFunc), new object[] { Ctrl });
        }

        /// <summary>
        /// 设置单选按钮选中状态方法
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Checked"></param>
        private static void SetRadioButtonCheckedFunc(RadioButton Ctrl, bool Checked)
        {
            Ctrl.Checked = Checked;
            Ctrl.Refresh();
        }

        /// <summary>
        /// 设置单选按钮选中状态
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Checked"></param>
        public static void SetRadioButtonChecked(RadioButton Ctrl, bool Checked)
        {
            Ctrl.Invoke(new SetRadioButtonCheckedDelegate(SetRadioButtonCheckedFunc), new object[] { Ctrl, Checked });
        }
    }
}

进度条(ProgressBar)
ProgressBarInvoke.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace XY.WINUI.ControlEx.InvokeEx
{
    /// <summary>
    /// ProgressBarInvoke
    /// </summary>
    public class ProgressBarInvoke
    {
        /// <summary>
        /// 获取进度条当前值委托
        /// </summary>
        /// <param name="Ctrl"></param>
        private delegate int GetProgressBarValueDelegate(ProgressBar Ctrl);
        /// <summary>
        /// 设置进度条当前值委托
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        private delegate void SetProgressBarValueDelegate(ProgressBar Ctrl, int Value);

        /// <summary>
        /// 获取进度条最小值委托
        /// </summary>
        /// <param name="Ctrl"></param>
        private delegate int GetProgressBarMinValueDelegate(ProgressBar Ctrl);
        /// <summary>
        /// 设置进度条最小值委托
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        private delegate void SetProgressBarMinValueDelegate(ProgressBar Ctrl, int Value);

        /// <summary>
        /// 获取进度条最大值委托
        /// </summary>
        /// <param name="Ctrl"></param>
        private delegate int GetProgressBarMaxValueDelegate(ProgressBar Ctrl);
        /// <summary>
        /// 设置进度条最大值委托
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        private delegate void SetProgressBarMaxValueDelegate(ProgressBar Ctrl, int Value);

        /// <summary>
        /// 获取进度条当前值方法
        /// </summary>
        /// <param name="Ctrl"></param>
        private static int GetProgressBarValueFunc(ProgressBar Ctrl)
        {
            return Ctrl.Value;
        }

        /// <summary>
        /// 获取进度条当前值
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <returns></returns>
        public static int GetProgressBarValue(ProgressBar Ctrl)
        {
            return (int)Ctrl.Invoke(new GetProgressBarValueDelegate(GetProgressBarValueFunc), new object[] { Ctrl });
        }

        /// <summary>
        /// 设置进度条当前值方法
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        private static void SetProgressBarValueFunc(ProgressBar Ctrl, int Value)
        {
            Ctrl.Value = Value;
            Ctrl.Refresh();
        }

        /// <summary>
        /// 设置进度条当前值
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        public static void SetProgressBarValue(ProgressBar Ctrl, int Value)
        {
            Ctrl.Invoke(new SetProgressBarValueDelegate(SetProgressBarValueFunc), new object[] { Ctrl, Value });
        }

        /// <summary>
        /// 获取进度条最小值方法
        /// </summary>
        /// <param name="Ctrl"></param>
        private static int GetProgressBarMinValueFunc(ProgressBar Ctrl)
        {
            return Ctrl.Minimum;
        }

        /// <summary>
        /// 获取进度条最小值
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <returns></returns>
        public static int GetProgressBarMinValue(ProgressBar Ctrl)
        {
            return (int)Ctrl.Invoke(new GetProgressBarValueDelegate(GetProgressBarMinValueFunc), new object[] { Ctrl });
        }

        /// <summary>
        /// 设置进度条最小值方法
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        private static void SetProgressBarMinValueFunc(ProgressBar Ctrl, int Value)
        {
            Ctrl.Minimum = Value;
            Ctrl.Refresh();
        }

        /// <summary>
        /// 设置进度条最小值
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        public static void SetProgressBarMinValue(ProgressBar Ctrl, int Value)
        {
            Ctrl.Invoke(new SetProgressBarValueDelegate(SetProgressBarMinValueFunc), new object[] { Ctrl, Value });
        }

        /// <summary>
        /// 获取进度条最大值方法
        /// </summary>
        /// <param name="Ctrl"></param>
        private static int GetProgressBarMaxValueFunc(ProgressBar Ctrl)
        {
            return Ctrl.Maximum;
        }

        /// <summary>
        /// 获取进度条最大值
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <returns></returns>
        public static int GetProgressBarMaxValue(ProgressBar Ctrl)
        {
            return (int)Ctrl.Invoke(new GetProgressBarValueDelegate(GetProgressBarMaxValueFunc), new object[] { Ctrl });
        }

        /// <summary>
        /// 设置进度条最大值方法
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        private static void SetProgressBarMaxValueFunc(ProgressBar Ctrl, int Value)
        {
            Ctrl.Maximum = Value;
            Ctrl.Refresh();
        }

        /// <summary>
        /// 设置进度条最大值
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="Value"></param>
        public static void SetProgressBarMaxValue(ProgressBar Ctrl, int Value)
        {
            Ctrl.Invoke(new SetProgressBarValueDelegate(SetProgressBarMaxValueFunc), new object[] { Ctrl, Value });
        }
    }
}

GitHub源码

由于时间关系,暂时就封装了这么多。

会持续更新……

欢迎大家补充。

WinForm跨线程UI操作常用控件类大全

posted @ 2016-05-24 12:41  CodeHsu  阅读(6004)  评论(33编辑  收藏  举报