Hello,
I just have a quick quesion about closing an application...
How can I make it so when I hit a command button, it closes all forms in my project, not just the one that has the command button?
Thanks for your help!
-Emo
Printable View
Hello,
I just have a quick quesion about closing an application...
How can I make it so when I hit a command button, it closes all forms in my project, not just the one that has the command button?
Thanks for your help!
-Emo
[Edited by CthulhuDragon on 08-31-2000 at 05:33 PM]Code:Private Sub Command1_Click()
dim x as form
for each x in forms
unload x
set x = nothing
next x
end sub
Use this:
Code:Public Sub UnloadAllForms()
Dim OfTheseForms As Form
For Each OfTheseForms In Forms
Unload OfTheseForms
Set OfTheseForms = Nothing
Next OfTheseForms
End Sub
Usage:
Call UnloadAllForms
Heh. My 3-liner beats yours!Code:For i = 0 to Forms.Count-1
Unload Forms(0)
Next i
I won! I was the fastest by a whole minute! :)
parksie, it is best if you Set each form to nothing. It will free up any resources that the form is using. CthulhuDragon, you have Set x = nothing at after the Next, so it will set the last form to nothing, not every form. And an End statement can be added after next.
Code:Public Sub UnloadAllForms()
Dim OfTheseForms As Form
For Each OfTheseForms In Forms
Unload OfTheseForms
Set OfTheseForms = Nothing
Next OfTheseForms
End
End Sub
Parksie:
You don't win..you didn't set the forms = nothing
which is not necessary but good programming practice.
Least ways, that's what I was told.
:D
If you close all the forms then your app will end, therefore automatically freeing the resources. Anyway - the resources are freed when the form is unloaded.
Actually since the app is closing it doesn't matter all that much. It really only makes a difference when the app is going to be around for a while eating up resources. But habit prevails.
The line Set X = Nothing should come before the line Next X, otherwise it's not in the loop.Quote:
Originally posted by CthulhuDragon
Code:Private Sub Command1_Click()
dim x as form
for each x in forms
unload x
next x
set x = nothing
end sub
Megatron - I know it was a typo. :)
As HeSaidJoe said, you don't have to use it, but it's a good habit.
Unload Me won't always free up resources, so you should really use Set form = Nothing to free up those few resources that are running.
And parksie, code fix:
Code:For i = 0 to Forms.Count-1
Unload Forms(i) 'for i(index) = 0 to forms.count, you have 0 :)
Next i
...and the beat goes on...
Later
:D
Actually, if you keep it at Forms(0), it will unload the next form - since when you unload a form, it removes it from the collection. Therefore Forms(0) will cycle through them all :).
Agreed, if your forms were an array then i would be the correct index but since it's a collection i would never work.
Now, this thread is officially dead! Least we hope so :D
When WM_DESTROY and WM_NCDESTROY are set to the window, they will automatically free up resources. Using the statement Set Form = Nothing is simply a process of double checking.Quote:
Originally posted by Matthew Gates
As HeSaidJoe said, you don't have to use it, but it's a good habit.
Unload Me won't always free up resources, so you should really use Set form = Nothing to free up those few resources that are running.
And parksie, code fix:
Code:For i = 0 to Forms.Count-1
Unload Forms(i) 'for i(index) = 0 to forms.count, you have 0 :)
Next i