将文本发送到Form1

时间:2017-01-23 14:48:13

标签: c# winforms

您好我如何将Program.cs中收到的文字发送到ListBox上的Form1

if (text == "alerts-")
{
    Task.Run(() => Application.Run(new Form1()));
    string[] text = text.Split('-');

    Form1.listBox1.Items.Add("Recived" + DateTime.Now.ToString() + "> " + text);
}

1 个答案:

答案 0 :(得分:1)

由于在创建表单之前有文本,并且由于表单需要文本,因此请将其作为参数包含在表单的构造函数中。

在表单本身中,添加构造函数参数:

public Form1(string[] text)
{
    // do whatever you need to do with the text on Form1
}

然后在创建表单实例时发送它:

Task.Run(() => Application.Run(new Form1(text.Split('-'))));