C# 将文本发送到Form1
您好,我如何将C# 将文本发送到Form1,c#,winforms,C#,Winforms,您好,我如何将程序.cs中收到的文本发送到表单1上的列表框 if (text == "alerts-") { Task.Run(() => Application.Run(new Form1())); string[] text = text.Split('-'); Form1.listBox1.Items.Add("Recived" + DateTime.Now.ToString() + "> " + text); } 因为在创建表单之前已经有了文本,而
程序.cs中收到的文本发送到表单1上的列表框
if (text == "alerts-")
{
Task.Run(() => Application.Run(new Form1()));
string[] text = text.Split('-');
Form1.listBox1.Items.Add("Recived" + DateTime.Now.ToString() + "> " + text);
}
因为在创建表单之前已经有了文本,而且表单需要文本,所以在表单的构造函数中将其作为参数包含
在表单本身中,添加构造函数参数:
public Form1(string[] text)
{
// do whatever you need to do with the text on Form1
}
然后在创建表单实例时发送:
Task.Run(() => Application.Run(new Form1(text.Split('-'))));
怎么了?为什么在单独的线程中运行窗体?访问非静态成员Form1.listBox1.Items需要对象引用。AddForm1
是类,而不是对象。这样做:form1f=newform1();Task.Run(()=>Application.Run(f));。。。f、 listBox1.Items.Add(“Recived”+DateTime.Now.ToString()+“>”+text)代码>现在我需要做一些事情,这样我就不会每次发送消息时都打开一个新窗口-->