我尝试从多个RichTextBox
更新static methods
控件。每次都会在非常快的时间间隔内添加大量文本。这是Client-Sever类应用程序,dll正在使用静态处理程序。我使用以下代码,并且在静态方法的RichTextBox中没有添加任何文本。我该如何解决这个问题?
public partial class MyMainForm : Form
{
private static MyMainForm FrmReference;
private static void Handler1(object parameter, ConnectionEvent CE)
{
FrmReference.MyRichTextBox.AppendText(DateTime.Now + "....x....");
}
private static void Handler2(object parameter, DataEvent DE)
{
FrmReference.MyRichTextBox.AppendText(DateTime.Now + "....y....");
}
public MyMainForm()
{
InitializeComponent();
FrmReference = this;
}
private void VNToolStripButtonStart_Click(object sender, EventArgs e)
{
con = new Connection("127.0.0.1", ConParams);
con.SetConnectionHandler(Handler1, null);
con.SetDataHandler(Handler2, null);
MyRichTextBox.AppendText(DateTime.Now + "....abc....");
}
}
答案 0 :(得分:1)
您的处理程序未被调用。
如果你尝试这样的话,你会看到问题不是应该写入RichTextBox
的静态方法:
public partial class Form1 : Form
{
private static Form1 frmReference;
private static void WriteToMyRichTextBox(string what)
{
frmReference.richTextBox1.AppendText(what);
frmReference.richTextBox1.AppendText(Environment.NewLine);
}
public Form1()
{
InitializeComponent();
frmReference = this;
}
private void button1_Click(object sender, EventArgs e)
{
WriteToMyRichTextBox(DateTime.Now.ToString());
}
}