如何在新线程启动的类中对 RichTextBox.Text 的改写.............

kissxrl 2007-11-13 04:23:41
using System;
using System.Windows.Forms;

namespace frm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Thread thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(this.threadstart));
thread1.Start();
}

private void threadstart()
{
Common.logView log = new Common.logView();
log.richTextBox1 = this.richTextBox1;
log.editText();
}
}
}


namespace Common
{
public class logView
{
public RichTextBox richTextBox1 = new RichTextBox();

delegate void delegateSetLogTextBox(string text, bool color);

public void editText()
{
this.richTextBox1.Text += "在新线程启动的类中对 RichTextBox.Text 的改写";
}
}
}


上面是我的代码,我知道在线程中操作控件需要用 delegate 托管代码......可是我的是在线程启动的类的中操作控件...我在上面的例子中只启动呢一个线程..实际的程序中应该是1个以上的线程..

delegate void delegateSetTextBox(string text);
private void SetTextBox(string text)
{
if (this.richTextBox1.InvokeRequired)
{
delegateSetTextBox d = new delegateSetTextBox(this.SetTextBox);
this.Invoke(d, new object[] { text });
}
else
{
this.richTextBox1.AppendText(text);
}
}

以上的代码..不知道如何用到我上面的代码中......
...全文
146 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kissxrl 2007-11-13
  • 打赏
  • 举报
回复

刚写错呢..多谢 zlc_168
kissxrl 2007-11-13
  • 打赏
  • 举报
回复

不行呀....

==================

错误 1
“delegateSetTextBox”: 无法通过表达式引用类型;请尝试“Common.logView.delegateSetTextBox” D:\USER\My Documents\Visual Studio 2005\Projects\WindowsApplication4\WindowsApplication4\Form1.cs 22 17 Accp.Common

===================

我的代码...


using System;
using System.Windows.Forms;

namespace frm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Thread thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(this.threadstart));
thread1.Start();
}

private void threadstart()
{
Common.logView log = new Common.logView();
log.delegateSetTextBox = new delegateSetTextBox(SetTextBox);
log.richTextBox1 = this.richTextBox1;
log.editText();
}

delegate void delegateSetTextBox(string text);
}
}


namespace Common
{
public class logView
{
public RichTextBox richTextBox1;
public delegate void delegateSetTextBox(string text);

public void editText()
{
this.richTextBox1.Text += "在新线程启动的类中对 RichTextBox.Text 的改写";
}
}
}
kissxrl 2007-11-13
  • 打赏
  • 举报
回复
多谢 zlc_168 偶去试一下
zlc_168 2007-11-13
  • 打赏
  • 举报
回复
另:public RichTextBox richTextBox1 = new RichTextBox();
不必要创建实例,因为richTextBox1 只是对form1的RichTextBox引用
zlc_168 2007-11-13
  • 打赏
  • 举报
回复
在方法 threadstart中加入下列代码:
private void threadstart()
{
....
log.delegateSetTextBox = new delegateSetTextBox(SetTextBox);
}
在类Form1中加入下列方法:
private void SetTextBox(string text)
{
this.richTextBox1.Text = text;
}

在类logView的方法中进行异步调用
public void editText()
{
object[] datas = { "在新线程启动的类中对 RichTextBox.Text 的改写 "};
richTextBox1.Invoke(delegateSetLogTextBox,datas);
}


110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧