12

Which is a better way to show a modal dialog?

form1 frm=new form1();
frm.showDialog()

or

(new form1()).showDialog();
Cody Gray
  • 222,280
  • 47
  • 466
  • 543
Stack Overflow
  • 159
  • 1
  • 2
  • 6

2 Answers2

43

Neither one is "better" than the other; they are perfectly equivalent!

However, in this particular case, both are wrong. The ShowDialog method requires you to call the Dispose method on the form. Unlike the Show and Close combination, this is not done automatically. From MSDN:

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

Thus, you should choose between one of these (equivalent) forms:

using (Form1 frm = new Form1())
{
    frm.ShowDialog();
}

or

Form1 frm = new Form1();
frm.ShowDialog();
frm.Dispose();

The reason that ShowDialog doesn't automatically dispose the form is simple enough, if not immediately obvious. It turns out that applications often wish to read values from an instance of a modal dialog form after the form has been closed, such as settings specified in the form's controls. If the form were automatically disposed, you would be unable to read those values by accessing properties of the form object. Thus, the programmer is responsible for disposing forms shown as modal dialogs when (s)he is finished with them.

Cody Gray
  • 222,280
  • 47
  • 466
  • 543
  • Excellent clarification on why MODAL keeps the form in memory to extract the other elements upon its closure and not auto-released. – DRapp Dec 24 '11 at 13:25
  • Just to clarify: if the form instance is held in a local variable it will be eligible for garbage collection when it goes out of scope and will *eventually* be disposed (see this SO [answer](http://stackoverflow.com/a/11690188/945456)). Still a good idea to do it explicitly of course. – Jeff B Feb 12 '14 at 19:29
  • Except the rest, the very first sentence is **wrong**. Since you must/should call `frm.Dispose();` the 2nd one is worse than the first one. _What a shame_ in view of the good answer afterwards! – Bitterblue Feb 25 '14 at 11:32
  • 1
    The two "forms" are not equivalent if you consider exceptions. – cic Jan 31 '15 at 18:04
  • It's too bad MS didn't encourage disposable objects to support methods and properties that could indicate what their state was when disposed, in cases where such properties could be implemented without holding any resources. IMHO it would have been cleaner to release the resources associated with a form's controls immediately when it is closed, but be able to ask its members what they held at that time, than to have forms base the decision of whether or not to release resources on the means by which they were shown. – supercat Feb 02 '15 at 18:02
  • If you are reusing a Form, you don't have to Dispose() it and create it again. [[[ Form2 f = new Form2(); ... f.ShowDialog(); ... f.ShowDialog(); ... f.Dispose(); ]]] -or- [[[ using (Form2 f = new Form2()) { ... f.ShowDialog(); ... f.ShowDialog(); ... } ]]] – A876 Aug 04 '17 at 00:19
2

Generally I would go for the first 1 because you can access the form afterwards.

Otherwise the 2nd on is ok, if you dont want to deal with it after it is closed.

Adam
  • 15,596
  • 5
  • 63
  • 102
  • are you asking if the memory is disposed after the showdialog has finished. If so, then he memory for any object in c# is disposed once nothing references it anymore. So once the dialog is closed all references are gone and the GC will do its thing. – Adam Dec 24 '11 at 13:03