By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 2,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

Disposing of Windows Forms

P: n/a
I have a project that has 5 or 6 forms.
VB.NET VS 2005
In the FormClosing Event of each form I have the following code:

If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()

If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()

If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()

If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()

Me.Dispose()

In Form B I have to remove the: If Not IsNothing(frmB) Or Not
frmB.IsDisposed Then frmB.Close()

In Form C I have to remove the: If Not IsNothing(frmC) Or Not
frmC.IsDisposed Then frmC.Close()

etc.... Is there a way to iterate through all the forms in the FormClosing
event to make sure everything is closed before ending the application?

Thanks
Nov 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
What are you trying to accomplish?

Are you calling these in the respective forms, i.e. you're doing "If Not
IsNothing(frmA)..." in frmA, and "If Not IsNothing(frmB)..." in frmB and so
on, just to close the forms? Because you don't need to.

Presumably if you are *in* the FormClosing event, someone has chosen to
close the form, and it is being closed. At that point, asking to close it
would be repetitive.

Unlike older versions of VB, you no longer have to set the form to Nothing
in order to let the system know it is no longer needed. VB2005 marks it as
"unused" when there are no longer any references to it, and the
GarbageCollector gets rid of it on its next sweep through. This happens to
most objects, be it a form or a variable or a class object, whenever it goes
out of scope and there are no references to it.

Hope that helps.
Robin

"Henry Jones" <he***@yada.comwrote in message
news:eg****************@TK2MSFTNGP02.phx.gbl...
>I have a project that has 5 or 6 forms.
VB.NET VS 2005
In the FormClosing Event of each form I have the following code:

If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()

If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()

If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()

If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()

Me.Dispose()

In Form B I have to remove the: If Not IsNothing(frmB) Or Not
frmB.IsDisposed Then frmB.Close()

In Form C I have to remove the: If Not IsNothing(frmC) Or Not
frmC.IsDisposed Then frmC.Close()

etc.... Is there a way to iterate through all the forms in the
FormClosing event to make sure everything is closed before ending the
application?

Thanks


Nov 2 '06 #2

P: n/a
Yes, I am calling these respective forms in each form. In FormA I am
calling the FormB, FormC, FormD statements
In FormB I am Calling A,C,D, etc... So what you are saying is that I don't
need to call these when I am closing down the Program?

All that work for nothing :-)
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:2a******************************@comcast.com. ..
What are you trying to accomplish?

Are you calling these in the respective forms, i.e. you're doing "If Not
IsNothing(frmA)..." in frmA, and "If Not IsNothing(frmB)..." in frmB and
so on, just to close the forms? Because you don't need to.

Presumably if you are *in* the FormClosing event, someone has chosen to
close the form, and it is being closed. At that point, asking to close it
would be repetitive.

Unlike older versions of VB, you no longer have to set the form to Nothing
in order to let the system know it is no longer needed. VB2005 marks it as
"unused" when there are no longer any references to it, and the
GarbageCollector gets rid of it on its next sweep through. This happens to
most objects, be it a form or a variable or a class object, whenever it
goes out of scope and there are no references to it.

Hope that helps.
Robin

"Henry Jones" <he***@yada.comwrote in message
news:eg****************@TK2MSFTNGP02.phx.gbl...
>>I have a project that has 5 or 6 forms.
VB.NET VS 2005
In the FormClosing Event of each form I have the following code:

If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()

If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()

If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()

If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()

Me.Dispose()

In Form B I have to remove the: If Not IsNothing(frmB) Or Not
frmB.IsDisposed Then frmB.Close()

In Form C I have to remove the: If Not IsNothing(frmC) Or Not
frmC.IsDisposed Then frmC.Close()

etc.... Is there a way to iterate through all the forms in the
FormClosing event to make sure everything is closed before ending the
application?

Thanks



Nov 3 '06 #3

P: n/a
No effort is useless if you learn something from it. (Now lift your feet,
it's getting deep in here.)

Generally, you might have a main form, and have it close everything before
it exits, just to make sure you didn't leave anything open. But you
shouldn't close each form in its own close event.

I have a main form (MainForm - how imaginative), and I might check and close
all other forms like the following. This is assuming you don't want to
finish closing the main form until all the other ones are closed.

For Each myForm as Form in My.Application.OpenForms
'MainForm is closing, so don't fire the close event again
if myForm.Name <"MainForm" Then
myForm.Close
End If
Next
'do other stuff before finally letting MainForm close

If your app starts up with a Sub Main(), you would put the following code
there. If you wanted to put it in every form, or you didn't care what order
they were closed, you could also do the following, but put it in the
FormClosed event rather than FormClosing, though, because FormClosed fires
AFTER the form is closed.

For Each myForm as Form in My.Application.OpenForms
myForm.Close
Next

By the way, if those are child forms of an MDI parent, the parent closes
them when the parent is closed.

Hope that gives you some closure. (Sorry; couldn't resist.)
Robin

"Henry Jones" <he********@yada.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Yes, I am calling these respective forms in each form. In FormA I am
calling the FormB, FormC, FormD statements
In FormB I am Calling A,C,D, etc... So what you are saying is that I
don't need to call these when I am closing down the Program?

All that work for nothing :-)
"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:2a******************************@comcast.com. ..
>What are you trying to accomplish?

Are you calling these in the respective forms, i.e. you're doing "If Not
IsNothing(frmA)..." in frmA, and "If Not IsNothing(frmB)..." in frmB and
so on, just to close the forms? Because you don't need to.

Presumably if you are *in* the FormClosing event, someone has chosen to
close the form, and it is being closed. At that point, asking to close it
would be repetitive.

Unlike older versions of VB, you no longer have to set the form to
Nothing in order to let the system know it is no longer needed. VB2005
marks it as "unused" when there are no longer any references to it, and
the GarbageCollector gets rid of it on its next sweep through. This
happens to most objects, be it a form or a variable or a class object,
whenever it goes out of scope and there are no references to it.

Hope that helps.
Robin

"Henry Jones" <he***@yada.comwrote in message
news:eg****************@TK2MSFTNGP02.phx.gbl...
>>>I have a project that has 5 or 6 forms.
VB.NET VS 2005
In the FormClosing Event of each form I have the following code:

If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close()

If Not IsNothing(frmB) Or Not frmB.IsDisposed Then frmB.Close()

If Not IsNothing(frmC) Or Not frmC.IsDisposed Then frmC.Close()

If Not IsNothing(frmD) Or Not frmD.IsDisposed Then frmD.Close()

Me.Dispose()

In Form B I have to remove the: If Not IsNothing(frmB) Or Not
frmB.IsDisposed Then frmB.Close()

In Form C I have to remove the: If Not IsNothing(frmC) Or Not
frmC.IsDisposed Then frmC.Close()

etc.... Is there a way to iterate through all the forms in the
FormClosing event to make sure everything is closed before ending the
application?

Thanks




Nov 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.