0
点赞
收藏
分享

微信扫一扫

C#之线程、委托,强强联手操作窗体控件...


写本程序的目的,是为了理解委托

...



效果截图如下:

C#之线程、委托,强强联手操作窗体控件..._文本框




主要代码如下:

//线程处理函数
        public void threadProc()
        {
            setTextBox("测试成功");
        }

        //声明委托
        delegate void myDelegate(String text);

        //实现委托方法
        public void setTextBox(String text)
        {
             //判断是否要调用文本框对象的invoke方法
             if(this.textBox.InvokeRequired)
             {
                 //创建委托对象
                 myDelegate degSetText = new myDelegate(setTextBox);

                 //调用文本框对象的invoke方法
                 this.textBox.Invoke(degSetText, new object[] { text });
             } 
             else
             {
                 this.textBox.Text = text;
             }
        }





举报

相关推荐

0 条评论