【问题标题】:Establish Owner On New Instance Of Form在新的表单实例上建立所有者
【发布时间】:2011-10-30 05:45:18
【问题描述】:

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

//this Instance From main window
CashDeposit cd=new CashDeposit();
cd.Show(this);

现在我将关闭它并尝试在 CashDeposit 的新 EventHandller 上创建相同的新实例,如下所示:

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.

所以我很难从同一类中创建 CashDeposit 新实例的所有者,因为参考表单正在处理并且不知道如何从 CashDeposit 类中创建主窗口窗体和 CashDeposit 之间的新关系在相同的新实例上。

这里的主窗体是 CashDeposit 的所有者。在如上所述处理旧的(关系)表格后,我正试图在 CashDeposit 的新实例上建立所有者。

任何人都知道如何实现相同的目标?

【问题讨论】:

    标签: c# winforms visual-studio


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      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.
      }
      

      【讨论】:

        【解决方案3】:

        这里我问了关于在某个 EventHandler 的新实例上将 Main Form 作为 Owner 建立 CashDeposit 类的问题,它可能是 Button 或 TextboxKeyPress。

        查看以下代码:

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

        CashDeposit 类中的上述代码 Eastblished 作为自身实例的所有者,我想将主表单作为新实例上 CashDeposit 的所有者进行 Eastblished,因此我更喜欢使用以下解决问题的代码。

            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 属性,它显示了活动窗体的所有者,并且很好地处理了主窗口窗体和模型窗口窗体。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-17
          • 2013-08-21
          • 2023-03-17
          • 2020-10-10
          相关资源
          最近更新 更多