Home All Groups Group Topic Archive Search About

Disposing of Windows Forms

Author
2 Nov 2006 10:23 PM
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

Author
2 Nov 2006 10:39 PM
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

Show quoteHide quote
"Henry Jones" <he***@yada.com> wrote in message
news:eg%23H71s$GHA.3396@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
>
>
Author
3 Nov 2006 12:56 AM
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 :-)


Show quoteHide quote
"RobinS" <RobinS@NoSpam.yah.none> wrote in message
news:2aWdndQBbKky7tfYnZ2dnUVZ_qqdnZ2d@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.com> wrote in message
> news:eg%23H71s$GHA.3396@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
>>
>>
>
>
Author
3 Nov 2006 3:43 AM
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

Show quoteHide quote
"Henry Jones" <henryjo***@yada.com> wrote in message
news:%236pkOLu$GHA.4060@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" <RobinS@NoSpam.yah.none> wrote in message
> news:2aWdndQBbKky7tfYnZ2dnUVZ_qqdnZ2d@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.com> wrote in message
>> news:eg%23H71s$GHA.3396@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
>>>
>>>
>>
>>
>
>