473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

where to dispose of form when not shown modal?

Suppose I have a button on a form that opens up another form. the code
in the buttons click event is:

frmMyCustomForm frm = new frmMyCustomForm ();

frm.ShowDialog();

frm.Dispose();

The form opens, user does what they need, the form closes, and gets
disposed immediately afterwards, due to the fact that the program is
stuck on the frm.ShowDialog() line until the form closess.

But suppose I don't want to do ShowDialog. I just want to use
frm.Show(). In that case, the program would continue to the next line
and I certainly wouldn't want to immediately dispose it. Where do I
dispose it?

Dec 8 '06 #1
5 4667
Hello,

you never need to dispose a Form, the garbage collector does it for you. You
can call Close() on the Form, that's it.

Bye

--
Matthias Pieroth
www.codegod.de - The Page for .NET-developers

<Mi************@gmail.comwrote in message
news:11*********************@73g2000cwn.googlegrou ps.com...
Suppose I have a button on a form that opens up another form. the code
in the buttons click event is:

frmMyCustomForm frm = new frmMyCustomForm ();

frm.ShowDialog();

frm.Dispose();

The form opens, user does what they need, the form closes, and gets
disposed immediately afterwards, due to the fact that the program is
stuck on the frm.ShowDialog() line until the form closess.

But suppose I don't want to do ShowDialog. I just want to use
frm.Show(). In that case, the program would continue to the next line
and I certainly wouldn't want to immediately dispose it. Where do I
dispose it?

Dec 8 '06 #2

Mi************@gmail.com wrote:
Suppose I have a button on a form that opens up another form. the code
in the buttons click event is:

frmMyCustomForm frm = new frmMyCustomForm ();

frm.ShowDialog();

frm.Dispose();

The form opens, user does what they need, the form closes, and gets
disposed immediately afterwards, due to the fact that the program is
stuck on the frm.ShowDialog() line until the form closess.

But suppose I don't want to do ShowDialog. I just want to use
frm.Show(). In that case, the program would continue to the next line
and I certainly wouldn't want to immediately dispose it. Where do I
dispose it?
A form shown using .Show() disposes of itself when the user closes it.
The only reason that forms shown using .ShowDialog() don't do this is
that they expect you may want to query them after the user presses OK
in order to get information out of them before they are disposed.

By the way, a useful pattern for dealing with forms that are to be
shown as dialogs is this:

using (frmMyCustomForm frm = new frmMyCustomForm ())
{
... set up dialog ...
frm.ShowDialog();
... get info back from dialog ...
}

Of course, if you're using .Show() this idiom isn't appropriate.

Dec 8 '06 #3

Matthias Pieroth wrote:
Hello,

you never need to dispose a Form, the garbage collector does it for you. You
can call Close() on the Form, that's it.
Microsoft disagrees with you: see the explicit instructions to
Dispose() a Form shown using ShowDialog:

http://msdn2.microsoft.com/en-us/library/c7ykbedk.aspx

You are, however, correct when a Form is shown using Show(): the Form
disposes of itself upon closing.

I believe (I could be wrong) that the issue here is when the window
handle and its associated resources are released back to the operating
system. The Dispose() method releases the Form's real O/S resources
without waiting for the GC. Since there is no guarantee as to when the
GC is going to run, it is preferable to Dispose() and release any O/S
resources used by the controls and the window sooner rather than later.

Although, it is true, the GC will eventually perform the same function
if you forget / don't bother.

Dec 8 '06 #4
Great info!

Thanks to all who replyed!

Bruce Wood wrote:
Mi************@gmail.com wrote:
Suppose I have a button on a form that opens up another form. the code
in the buttons click event is:

frmMyCustomForm frm = new frmMyCustomForm ();

frm.ShowDialog();

frm.Dispose();

The form opens, user does what they need, the form closes, and gets
disposed immediately afterwards, due to the fact that the program is
stuck on the frm.ShowDialog() line until the form closess.

But suppose I don't want to do ShowDialog. I just want to use
frm.Show(). In that case, the program would continue to the next line
and I certainly wouldn't want to immediately dispose it. Where do I
dispose it?

A form shown using .Show() disposes of itself when the user closes it.
The only reason that forms shown using .ShowDialog() don't do this is
that they expect you may want to query them after the user presses OK
in order to get information out of them before they are disposed.

By the way, a useful pattern for dealing with forms that are to be
shown as dialogs is this:

using (frmMyCustomForm frm = new frmMyCustomForm ())
{
... set up dialog ...
frm.ShowDialog();
... get info back from dialog ...
}

Of course, if you're using .Show() this idiom isn't appropriate.
Dec 8 '06 #5
Peter,

That is not correct.

When a form is closed (receives the WM_CLOSE windows message) the
implementation of Dispose is ultimately called.

That being said, it would be fine to handle unmanaged resources in your
form and override the Dispose method to properly take care of your unmanaged
resources.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:DF**********************************@microsof t.com...
Under normal conditions you will not need to worry about this. Just close
the
form and as long as it doesn't hold references to unmanaged resources that
haven't been properly disposed, it will be marked for garbage collection
and
handled by the runtime.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Mi************@gmail.com" wrote:
>Suppose I have a button on a form that opens up another form. the code
in the buttons click event is:

frmMyCustomForm frm = new frmMyCustomForm ();

frm.ShowDialog();

frm.Dispose();

The form opens, user does what they need, the form closes, and gets
disposed immediately afterwards, due to the fact that the program is
stuck on the frm.ShowDialog() line until the form closess.

But suppose I don't want to do ShowDialog. I just want to use
frm.Show(). In that case, the program would continue to the next line
and I certainly wouldn't want to immediately dispose it. Where do I
dispose it?


Dec 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kyralessa | last post by:
In Access 2000, I have a base form with a ListBox of conference registrants. In the form's declarations section I include Dim f as Form To add a registrant I'm doing this: Set f = New...
11
by: Pablo Salazar | last post by:
Hi people!!. Which is diference between to use method CLOSE and DISPOSE to close a form? Tx. Pablo Salazar
10
by: Phil | last post by:
Hello Can anybody tell me which events are fired immediately after a form is displayed i.e. appears on screen? Ta Phil
4
by: Paul Aspinall | last post by:
Can anyone advise how to display a form on top, and modal, without using ShowDialog?? Thanks
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
3
by: Tony Johansson | last post by:
Hello! I have a modal dialog lets call it TestDialog that is shown by using method showDialog(). This TestDialog has three controls it's one richtextbox and two buttons. The buttons is one Ok...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
5
by: Rob | last post by:
In Access there was a modal property of a form... it required that the current form be "answered" before getting to another form within the program. Is there such a property in VB.net ? If...
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.