C# 在表单的新实例上建立所有者

C# 在表单的新实例上建立所有者,c#,winforms,visual-studio,C#,Winforms,Visual Studio,是否可以在表单的新实例上建立所有者?。在使用主窗体和模型窗口时,我想到了一个问题,假设我创建一个新的Form1实例,如下所示: //this Instance From main window CashDeposit cd=new CashDeposit(); cd.Show(this); private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { this.Close(); CashDeposit cdd =

是否可以在表单的新实例上建立所有者?。在使用主窗体和模型窗口时,我想到了一个问题,假设我创建一个新的Form1实例,如下所示:

//this Instance From main window
CashDeposit cd=new CashDeposit();
cd.Show(this);
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
 this.Close();
 CashDeposit cdd = new CashDeposit();
 cdd.Show();
}
//this would showing without any owner but if I create the new instance on another way  like below:        

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
 this.Close();
 CashDeposit cdd = new CashDeposit();
 cdd.Show(this);
}

//than obviously it will going to fire the error like not creating owner on disposing  object or control etc.
现在,我将关闭同一个实例,并尝试在现金存款的新EventHandler上创建该实例的新实例,如下所示:

//this Instance From main window
CashDeposit cd=new CashDeposit();
cd.Show(this);
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
 this.Close();
 CashDeposit cdd = new CashDeposit();
 cdd.Show();
}
//this would showing without any owner but if I create the new instance on another way  like below:        

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
 this.Close();
 CashDeposit cdd = new CashDeposit();
 cdd.Show(this);
}

//than obviously it will going to fire the error like not creating owner on disposing  object or control etc.
因此,我很难从同一类中创建新的现金存款实例的所有者,因为参考表单正在处理,不知道如何在同一类的新实例上创建主窗口表单和现金存款类中的现金存款之间的新关系

这里的主窗体是现金存款的所有者。在处理了上面的旧的一个(关系)表单之后,我正在尝试向所有者介绍新的现金存款实例


任何人都知道如何做到这一点?

您可以通过更改以下代码(在您的
现金存款中)来解决您的问题


如果关闭主窗体,主线程将与所有子窗体一起关闭

您可以做的是隐藏主窗体并打开新的子窗体

this.Hide();
CashDeposit cdd = new CashDeposit();
cdd.FormClosed += new FormClosedEventHandler(cdd_FormClosed);
cdd.Owner = this.Owner;
cdd.Show();
即使为子窗体创建“关闭”,也可以同时关闭主窗体或重新打开主窗体

void cdd_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Show(); // or this.Close(); depend on your req.
}

在这里,我问了一个问题,关于在某个事件中,在同一个新实例上,将主窗体作为所有者建立到CashDeposit类中,它可能是Button或TextboxKeyPress

请看以下代码:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
   this.Close();
   CashDeposit cdd = new CashDeposit();
   cdd.Show(this.Owner);
}
CashDeposit类中的上述代码EastBlish是一个自我实例的所有者,因为我想在新实例中创建一个主要的表单作为CashDeposit的启动者,因此我更倾向于使用以下代码来解决问题

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
      this.Close();
      CashDeposit cdd = new CashDeposit();
      cdd.Show(MainForm.ActiveForm); //You can Replcae MainForm with your Orginal Form 

    }
现在,根据上面的内容,我刚刚添加了Form.ActiveForm属性,它显示了ActiveForm的所有者,并且可以很好地处理主窗口窗体和模型窗口窗体