首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

如果在线程中把收到的数据展示到窗体的文本框中

2012-06-23 
如果在线程中把收到的数据显示到窗体的文本框中我的代码如下:声明:delegate void SetTextCallback(string

如果在线程中把收到的数据显示到窗体的文本框中
我的代码如下:
声明:
delegate void SetTextCallback(string text);
在线程中调用
textBox1SetText(clientcommand);

为string 数据
代码:
private void textBox1SetText(string str)

  {

  if (this.rtb_note.InvokeRequired)
  {
  //WriteTxt(text);
  SetTextCallback d = new SetTextCallback(textBox1SetText);
  this.Invoke(d, new object[] { str });
  }
  else
  {
  this.rtb_note.Text = this.rtb_note.Text + "\r\n" + str;
  }
  }
为什么不能把信息加到rtb_note中显示呢。

[解决办法]
没问题,以下为测试代码

C# code
        delegate void SetTextCallback(string text);        private void textBox1SetText(string str)        {            if (this.textBox3.InvokeRequired)            {                SetTextCallback d = new SetTextCallback(textBox1SetText);                this.Invoke(d, new object[] { str });            }            else            {                this.textBox3.Text = this.textBox3.Text + "\r\n" + str;            }        }        private void Foo()        {            textBox1SetText("abc");        }        private void button10_Click(object sender, EventArgs e)        {            Thread t = new Thread(new ThreadStart(Foo));            t.Start();        } 

热点排行