Home All Groups Group Topic Archive Search About

Shut Down Program with Multiple Forms

Author
14 May 2005 3:26 PM
Jim Richards
Hello. I have a program that uses nine forms. Is there a way to Unload all
of them with a single line of code? Or do I have to use the Unload statement
nine times to be sure they are all unloaded? TIA. Jim.

Author
14 May 2005 3:44 PM
Bob Butler
"Jim Richards" <JWRicha***@satx.rr.com> wrote in message
news:uMohe.64461$AE6.12530@tornado.texas.rr.com
> Hello. I have a program that uses nine forms. Is there a way to
> Unload all of them with a single line of code? Or do I have to use
> the Unload statement nine times to be sure they are all unloaded?
> TIA. Jim. 

You have to unload each but you can use the forms collection

dim f as form
for each f in forms
  unload f
next

IMO it's sloppy coding, but it works

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
14 May 2005 11:27 PM
MikeD
Show quote
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:uQhjhvJWFHA.4056@TK2MSFTNGP15.phx.gbl...
> "Jim Richards" <JWRicha***@satx.rr.com> wrote in message
> news:uMohe.64461$AE6.12530@tornado.texas.rr.com
>> Hello. I have a program that uses nine forms. Is there a way to
>> Unload all of them with a single line of code? Or do I have to use
>> the Unload statement nine times to be sure they are all unloaded?
>> TIA. Jim.
>
> You have to unload each but you can use the forms collection
>
> dim f as form
> for each f in forms
>  unload f
> next
>
> IMO it's sloppy coding, but it works


Why do you consider that sloppy?

--
Mike
Microsoft MVP Visual Basic
Author
14 May 2005 11:57 PM
Bob Butler
Show quote
"MikeD" <nob***@nowhere.edu> wrote in message
news:eQdIAyNWFHA.3176@TK2MSFTNGP12.phx.gbl
> "Bob Butler" <tiredofit@nospam.com> wrote in message
> news:uQhjhvJWFHA.4056@TK2MSFTNGP15.phx.gbl...
>> "Jim Richards" <JWRicha***@satx.rr.com> wrote in message
>> news:uMohe.64461$AE6.12530@tornado.texas.rr.com
>>> Hello. I have a program that uses nine forms. Is there a way to
>>> Unload all of them with a single line of code? Or do I have to use
>>> the Unload statement nine times to be sure they are all unloaded?
>>> TIA. Jim.
>>
>> You have to unload each but you can use the forms collection
>>
>> dim f as form
>> for each f in forms
>>  unload f
>> next
>>
>> IMO it's sloppy coding, but it works
>
> Why do you consider that sloppy?

Because it's a seldgehammer approach.  It makes sense to me for MDI apps
where you may have some unknown number of copies of a form but for most apps
I prefer code that explicitly releases any resources it allocates.  Doing
the mass unload may mask a bug that causes a form to not be unloaded when it
should.  If you aren't keeping track of what you are doing then IMO it's
sloppy.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
15 May 2005 12:19 AM
MikeD
Show quote
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:eUz2ADOWFHA.2128@TK2MSFTNGP14.phx.gbl...
> "MikeD" <nob***@nowhere.edu> wrote in message
> news:eQdIAyNWFHA.3176@TK2MSFTNGP12.phx.gbl
>> "Bob Butler" <tiredofit@nospam.com> wrote in message
>> news:uQhjhvJWFHA.4056@TK2MSFTNGP15.phx.gbl...
>>> "Jim Richards" <JWRicha***@satx.rr.com> wrote in message
>>> news:uMohe.64461$AE6.12530@tornado.texas.rr.com
>>>> Hello. I have a program that uses nine forms. Is there a way to
>>>> Unload all of them with a single line of code? Or do I have to use
>>>> the Unload statement nine times to be sure they are all unloaded?
>>>> TIA. Jim.
>>>
>>> You have to unload each but you can use the forms collection
>>>
>>> dim f as form
>>> for each f in forms
>>>  unload f
>>> next
>>>
>>> IMO it's sloppy coding, but it works
>>
>> Why do you consider that sloppy?
>
> Because it's a seldgehammer approach.  It makes sense to me for MDI apps
> where you may have some unknown number of copies of a form but for most
> apps
> I prefer code that explicitly releases any resources it allocates.  Doing
> the mass unload may mask a bug that causes a form to not be unloaded when
> it
> should.  If you aren't keeping track of what you are doing then IMO it's
> sloppy.
>

I can't agree 100% with you on that. Maybe about 90%.

I do agree that forms should be unloaded when they are no longer needed.
That's just proper memory and resource management.  But I see nothing wrong
with (nor consider it sloppy) having that code in the main form's Unload
event. Even in a non-MDI app, there may be several forms loaded (perhaps
shown, perhaps not) when the main form gets closed. Hence, the app should
terminate.


--
Mike
Microsoft MVP Visual Basic
Author
15 May 2005 1:08 PM
Bob Butler
"MikeD" <nob***@nowhere.edu> wrote in message
news:Ol92FPOWFHA.3488@tk2msftngp13.phx.gbl
<cut>
> I can't agree 100% with you on that. Maybe about 90%.

<g>

> I do agree that forms should be unloaded when they are no longer
> needed. That's just proper memory and resource management.  But I see
> nothing wrong with (nor consider it sloppy) having that code in the
> main form's Unload event. Even in a non-MDI app, there may be several
> forms loaded (perhaps shown, perhaps not) when the main form gets
> closed.

Right, and the cleanup code should handle them individually, closing them in
any order necessary and calling any special code to handle form-related
resources, etc.  IMO that's an order of magnitude better than just blindly
unloading all forms.  It's not that I put a big emphasis on this - using the
loop is certainly better than nothing and probably quite sufficient in just
about every application.  I just consider it a much less rigorous approach
than doing specific cleanup operations.

> Hence, the app should terminate.

As all good apps should <g>

--
Reply to the group so all can participate
VB.Net: "Fool me once..."

AddThis Social Bookmark Button