命名空间:sharClass.uiHelper
功能:跨线程访问界面上的控件
功能详细说明:
如果在线程中想访问界面上的控件时,可以使用这个uiHelper类,它提供了一些常用控件的可以确保线程安全的访问方式,避免出现跨线程访问控件这样的错误出现。
有关跨线程访问控件的相关话题,童鞋们请参见下面的文章:
C#中Invoke的用法
常用函数列表:
public static string GetTextString(Control ctrl) public static string GetTextString(StatusBarPanel ctrl) public static void SetTextString(Control ctrl, string text) public static void SetTextString(Control ctrl, string text, Color backColor) public static void SetTextString(Control ctrl, string text, bool backColor, bool defaultColor = false) public static void SetTextString(StatusBarPanel ctrl, string text) public static void SetTextString(ToolStripStatusLabel ctrl, string text) public static void SetTextString(RichTextBox ctrl, string text, Color fontColor) public static void AppendTextString(Control ctrl, string text, bool AndCRLF = true) public static int GetTextLenght(RichTextBox ctrl) public static bool GetChecked(CheckBox ctrl) public static void SetChecked(CheckBox ctrl, bool status) public static void SetBackColor(Control ctrl, Color backColor) public static void SetBackColor(ToolStripStatusLabel ctrl, Color backColor) public static Color GetBackColor(Control ctrl) public static Color GetBackColor(ToolStripStatusLabel ctrl) public static void SetForeColor(ToolStripStatusLabel ctrl, Color foreColor) public static void SetForeColor(Control ctrl, System.Drawing.Color foreColor) public static Color GetForeColor(Control ctrl) public static Color GetForeColor(ToolStripStatusLabel ctrl) public static void SetValue(ProgressBar ctrl, int value) public static void SetValue(TrackBar ctrl, int value) public static void SetComboBoxItems(ComboBox ctrl, string[] items) public static void SetComboBoxItems(ComboBox ctrl, List<string> items) public static void SetControlEnable(Control ctrl, bool textEnable = true, bool buttonEnable = true)
参考例子:
using sharClass; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace uiHelperTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { new Thread(new ThreadStart(() => { uiHelper.AppendTextString(this.richTextBox1, "AppendTextString"+Environment.NewLine, true); uiHelper.SetTextString(this.richTextBox1, "SetTextString" + Environment.NewLine, Color.Red); uiHelper.SetTextString(this.richTextBox1, uiHelper.GetTextLenght(this.richTextBox1) + Environment.NewLine, Color.Blue); uiHelper.SetTextString(this.textBox1, "hello! lxy"); uiHelper.SetValue(this.progressBar1, 50); uiHelper.SetComboBoxItems(this.comboBox1, new string[] { "红色", "绿色", "蓝色" }); })).Start(); } } }
这个类成员都是静态函数,使用简单,就不多做解释了。
目前这个类可以操作的控件以及操作方法都比较少,勇哥以后有时间会不断的增强这个类。
---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


