2017-01-23 60 views
-1

嗨如何將Program.cs中收到的文本發送到Form1上的ListBox發送文本到Form1

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

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

什麼是錯的? –

+0

爲什麼你在單獨的線程中運行窗體? –

+0

需要訪問非靜態成員的對象引用Form1.listBox1.Items.Add –

回答

1

既然你有文本創建表單之前,由於形式要求的文本,包括它在窗體的構造函數的參數。創建窗體的實例時

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

然後發送:

在形式本身,添加構造函數參數

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