简体   繁体   中英

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

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

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

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.

Better to Close the first dialogue and then show the second one.

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

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

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.

You have to create the child independently from the parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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