【发布时间】:2014-03-25 22:26:48
【问题描述】:
我正在尝试通过单独的类访问表单的 RichTextBox 控件。
在我的表单类中...
delegate void SetTextCall(string s);
public void safeCall(string s)
{
if (this.richTextBox1.InvokeRequired)
{
SetTextCall d = new SetTextCall(safeCall);
this.Invoke(d, new object[] { s });
}
else this.richTextBox1.AppendText("From Applicator");
}
在不同的班级...
public void getMessages()
{
lock (lockObj)
{
Dictionary<string, List<string>> result = ScannerMessages
.GroupBy(k => k.Value)
.Where(grp => grp.Count() > 1)
.ToDictionary(grp => grp.First().Key, grp => grp.Skip(1).Select(k => k.Key).ToList());
if(result.Count > 0)
{
foreach(var key in result.Keys)
{
// I want to write to the rich textbox the key and the list accosiated with the key
// in the richtextbox on Form1
}
}
}
}
我尝试将文本框修改为公开,并尝试修改创建一个静态方法来调用。我不知道该怎么做。
【问题讨论】:
-
以前,您必须在第二个类中引用第一个表单类对象。
-
您的“safeCall”方法有什么问题?您应该在表单上引入方法以允许从外部进行操作。直接访问控件不是一个好习惯。