C#A类调用B类的方法,在方法中更新B类的控件

1.首先在B类中定义静态成员

public static B b=null;

其次,在B类构造函数中给静态成员初始化

 public B()
 {
     B = this;
     InitializeComponent();
  }

在A类中,调用更新B类控件的方法

B.b.Method("已通过");

2.如果当前方法所在的线程不是UI线程,需要使用Invoke方法将Method方法委托给UI线程执行。

private void Method(string strMsg)
{
    if (tb_con.InvokeRequired)
    {
        tb_con.Invoke(new MethodInvoker(() => Method(strMsg)));
    }
    else
    {
        tb_con.Text += Environment.NewLine + strMsg;
    }
}

相关推荐

  1. C#A调用B方法方法更新B

    2024-06-13 11:00:04       10 阅读
  2. Python成员方法

    2024-06-13 11:00:04       29 阅读
  3. Python(class)使用方法

    2024-06-13 11:00:04       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 11:00:04       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 11:00:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 11:00:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 11:00:04       20 阅读

热门阅读

  1. 注解 - @RequestPart

    2024-06-13 11:00:04       7 阅读
  2. 设计模式-原型模式

    2024-06-13 11:00:04       10 阅读
  3. PostgreSQL 数据类型详细说明

    2024-06-13 11:00:04       9 阅读
  4. Elasticsearch-IndexTemplate和DynamicTemplate 有什么区别

    2024-06-13 11:00:04       5 阅读
  5. 1分钟带你了解代付业务|代付业务简介

    2024-06-13 11:00:04       6 阅读