简体   繁体   English

在this.Dispose()之后使用form.ShowDialog();

[英]Using form.ShowDialog() after this.Dispose();

I'm launching a form from another form. 我正在从另一个表单启动一个表单。 I want the parent form to be disposed before showing the subform, so I use: 我希望在显示子表单之前先处理父表单,所以我使用:

 this.Dispose();
 form.ShowDialog(); 

The form is displayed, is this the correct way to do it? 显示表格,这是正确的方法吗? The form from is launched by a call to ShowDialog() from the main form. 通过调用主窗体中的ShowDialog()启动窗体from。 I want these forms to be closed before the main form is editable. 我希望在主表单可编辑之前关闭这些表单。

EDIT:

This is the Basic Process 这是基本过程

Mainform>(Showdialog)>form1>(dispose+showDialog)>form2(dispose)>Mainform 的MainForm>(ShowDialog的)> Form1中>(处置+的ShowDialog)>窗口2(处置)>的MainForm

If you want to ensure the parent is not visible before the child is shown, AND you want the parent to Close: 如果要确保父级在显示子级之前不可见,并且希望父级关闭:

var f = new ChildForm();
this.Hide();
f.Show();
this.Close();

ShowDialog() means, "display this form and return to me after it's closed". ShowDialog()意思是“显示此表单并在关闭后返回给我 ”。 That is not what you described you want to happen. 那不是您想发生的事情。

Based on your comment, it might make more sense to create and show form2 from your main form. 根据您的评论,从主窗体创建并显示form2可能更有意义。

In the main form: 主要形式:

   DialogResult form1Result;
     using (var f = new Form1())
     {
        form1Result = f.ShowDialog();
     }
     if (form1Result == DialogResult.OK)
     {
        using (var f2 = new Form2())
        {
           f2.ShowDialog();
        }
     } 

And in Form1 event where you originally spawned Form2 在最初生成Form2的Form1事件中

this.DialogResult = DialogResult.OK;
this.Close();

Calling Dispose on any object (including the current object known as this ) is saying you have finished with that object, it is terminally finished with. 在任何对象(包括当前称为this对象)上调用Dispose表示您已经完成了该对象,并且最终完成了该对象。

Better to Close the first dialogue and then show the second one. 最好Close第一个对话框,然后显示第二个对话框。

If you are showing the child dialog modally (which you are in this case) then you can hide the parent dialog What you can do is hide the parent form, show the child form, and then close the parent form. 如果以模态显示子对话框(在本例中是这种情况),则可以隐藏父对话框。您可以做的是隐藏父窗体,显示子窗体,然后关闭父窗体。

var form = new SomeForm();
this.Hide();
form.ShowDialog(); // the code will be blocked at this point until the child form closes
this.Close();
this.Dispose(); // i'm not sure if this is necessary anymore. check MSDN

If you are showing a form (not modally), you can hide the parent form, and listen to the child form's close event and then close the parent form in the handler 如果显示的是表单(非模态),则可以隐藏父表单,并侦听子表单的close事件,然后在处理程序中关闭父表单。

void childFormClosed(Event e) 
{
    this.Close();
}

var form = new SomeForm();
form.Closed += new EventHandler... // I usually rely on intellisense with this
this.Hide();
form.ShowDialog();

sorry, I think you cant do that, because when you call ShowDialog method, the child form becomes dependent on its parent form. 抱歉,我认为您不能这样做,因为当您调用ShowDialog方法时,子窗体将依赖于其父窗体。 so the call of dispose will be suspended until you finish with the child form. 因此,处置请求将被暂停,直到您完成子表单为止。

I think you have to call the "Show" method and after that you can call the dispose method. 我认为您必须调用“ Show”方法,然后才能调用dispose方法。

The lifecycle of the child form is depends on its parent lifecycle, that means, once you invoke the parent.close of the parent form, it will dispose all objects created by that instance. 子窗体的生命周期取决于其父窗体的生命周期,这意味着,一旦调用父窗体的parent.close,它将处理该实例创建的所有对象。

You have to create the child independently from the parent. 您必须独立于父代创建子代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM