Disposing of Windows Forms

H

Henry Jones

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
 
R

RobinS

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
 
H

Henry Jones

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 :)
 
R

RobinS

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top