我有一個MainForm在哪裏打開另一個窗體,現在我有一個類,它提供了一些函數,我寫了一個函數獲取主窗體的引用和打開窗體的引用和其他參數我打開窗體中的函數調用,並引用MainForm我使用this.Parent,但我得到錯誤「對象引用沒有設置在一個opject的實例」。引用父窗體從子窗體
*客戶方是我的MainForm *登錄名是我在MainForm的打開和形式,其中我調用該方法RunListener
class ServicesProvider
{
public static void RunListener(ClientSide MainForm,LogIn LogForm,System.Net.Sockets.TcpClient Client)
{
//Doing my things with the parameters
}
}
這個代碼是在登錄表單
private void BtLogIn_Click(object sender, EventArgs e)
{
Thread Listener = new Thread(delegate()
{
ServicesProvider.RunListener((ClientSide)this.Parent,this,tcpClient);
});
Listener.Start();
}
的問題是,每當我調試我得到我告訴你的錯誤,我發現代碼「(ClinetSide)this.parent」引用null。 我需要參考主窗體來處理它並更改一些值。
你顯示子窗體像這樣:? 'child.Show(parent);'或'child.ShowDialog(parent);' –
LogIn LogForm = new LogIn(); LogForm.ShowDialog(); 這樣的 –
thax @PeterRitchie你啓發了我,我改變了LogIn窗體構造函數,讓它得到一個ClientSide參數。 LogIn LogForm = new LogIn(this); LogForm.showDialog(); –