我有一个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(); –