C# 跨线程更新用户控件UI

C#  跨线程更新用户控件UI
下面代码 主要实现 在类中的跨线程触发用户控件的按钮事件


1,类中定义委托
 public delegate void ClickThisButton(int b);
  public ClickThisButton clickbutton;
  public  int comorder ;// 定义对应用户控件的序号
 线程中调用:clickbutton(comorder);
  
------------------------------------------
2,MainForm 中定义
  public int comusing = 0; //用户控件序号


//可以添加多个用户控件,并对用户控件进行编号 
private void toolStripButton1_Click(object sender, EventArgs e)
        {
            comusing += 1;
            if (common.com.Count > 0 && comusing <= common.com.Count)
            { 
            if (comusing == 1)
            {
                toolStripTextBox1.Text = (common.com.Count - comusing).ToString();
                MyComPortControl mycomport = new MyComPortControl();
                mycomport.Top = 30;
                mycomport.comorder = comusing;
                mycomport.groupBox01.Text = comusing.ToString() + "号机位";
                mycomport.cbbComList01.Items.Add(common.com[comusing - 1]);
                mycomport.cbbComList01.Text = common.com[comusing - 1];
                SerialPort port = new SerialPort();
                mycomport.Comp = port;
                this.Height = (mycomport.Height + 70);
                this.Controls.Add(mycomport);
            }
            else {
                toolStripTextBox1.Text = (common.com.Count - comusing).ToString();
                MyComPortControl mycomport = new MyComPortControl();
                mycomport.Top = 30;
                mycomport.comorder = comusing;
                this.Width = (mycomport.Width + 20) * comusing;
                this.Height = (mycomport.Height + (comusing < 3 ? 70 : 15)) * (comusing < 3 ? 1 : 2);
                mycomport.Left = (mycomport.Width + 20) * (comusing == 1 ? 1 : comusing - 1);
                mycomport.groupBox01.Text = comusing.ToString() + "号机位";
                mycomport.cbbComList01.Items.Add(common.com[comusing - 1]);
                mycomport.cbbComList01.Text = common.com[comusing - 1];
                SerialPort port = new SerialPort();
                mycomport.Comp = port;
                this.Controls.Add(mycomport);
                }
            }
            
        }


-------------------------------------------
3,用户控件中定义
  public int comorder = 0;//用户控件序号,被添加时在Mianform 中赋值,
  delegate void AsynUpdateMyState(int b);
  //用户控件初始化时调用
   coms.clickbutton+=clickButton;


      public void clickButton(int b)
        {
            try
            {
                if (InvokeRequired)
                {
                    this.Invoke(new AsynUpdateMyState(delegate(int s)
                    {
                        for (int i = 0; i < common.userctls.Count; i++)
                        {
                            if (comorder  == b) 
                            {
                               ///用户中间中一个按钮
                               btnOpen01.PerformClick();
                            }
                        }
                    }), b);
                }
            }
            catch
            {


            }
          }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值