少有人走的路

勇哥的工业自动化技术网站

[netMarketing类库] uiHelper类:辅助跨线程访问控件

命名空间: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();
        }
    }
}

这个类成员都是静态函数,使用简单,就不多做解释了。

目前这个类可以操作的控件以及操作方法都比较少,勇哥以后有时间会不断的增强这个类。

image.png


--------------------- 

作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!

返回类库功能说明目录


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接

Powered By Z-BlogPHP 1.7.3

Copyright www.skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864