
Closing a form with the ‘X’ Button does not destroy in stances of Private Variables
From the
Visual Basic Books Online
Chapter - Life Cycle of Visual Basic Forms
Because theyre visible to the user, forms and controls have a different life
cycle than other objects. For example, a form will not close just because youve
released all your references to it. Visual Basic maintains a global collection
of all forms in your project, and only removes a form from that collection when
you unload the form.
In similar fashion, Visual Basic maintains a collection of controls on each
form. You can load and unload controls from control arrays, but simply releasing
all references to a control is not sufficient to destroy it.
For More Information The Forms and Controls collections are discussed in
Collections in Visual Basic earlier in this chapter.
Memory and Resources Completely Reclaimed
The only way to release all memory and resources is to unload the form and then
set all references to Nothing. The reference most commonly overlooked when doing
this is the hidden global variable mentioned earlier. If at any time you have
referred to the form by its class name (as shown in the Properties Window by the
Name property), youve used the hidden global variable. To free the forms
memory, you must set this variable to Nothing. For example:
Set Form1 = Nothing
(Put in the Form_Unload event to insure it gets called no matter how the form is
closed)
[VB5]
Robb
Remove autospam deterrent from address before emailing
On Wed, 14 Oct 1998 09:48:41 +0100, "Richard Allsebrook"
Quote:
>Closing a form with the X Button does not destroy Private Variables
>I've just stumbled across this little beauty. Is it a bug with VB6 or do other version of VB do this?
>It will take about 5 mins for you to reproduce this, but believe you me, it has far reaching consequences...
>Scenario 1:
>Two Forms (Form1, Form2)
>Form1 - Two command buttons (Command1, Command2)
>Sub Comand1_Click
> Form2.Show 'show form2
>End Sub
>Sub Command2_Click
> Unload Form2
> Set Form2=Nothing 'the "correct" method of closing a form
>End Sub
>Form2 - One Label (Label1), One Command Button (Command1) and a Privately declared Module level variable (strTest)
>Private strTest as String
>Sub Command1_Click
> strTest = strTest & "X"
> Label1=strTest
>End Sub
>Problem:
>1. Run the app.
>2. Click Command1 on Form1 (Displays Form2)
>3. Click the Command1 on Form2 (Appends 'X' to the end of private string and display it in Label1)
>4. Close form2 with the 'X' button on Form2 (Closes form2)
>5. Click Command1 on Form1 (Displays Form2 again)
>6. Click Command1 on Form2 (Appends 'X' to the end of private string and displays in Label1)
>What's wrong with this picture?
>Even though the form instance was destroyed, (it no longer exists in the Forms collection) my Private variable wasn't! The only place I can reference the variable (Form2) has been destroyed. So the variable just sits there in limbo somewhere until I launch Form2 again.
>What's the point of the Close button if it doesn't destroy the form instance and all its local variables? - It's really just a Form2.Hide that kills of the Form instance and its controls, but not its private variables!
>OK, lets try the "preferred" method of closing the form.
>Go trough steps 1 to 6 again, but for step 4 close Form2 using Command2 on Form1 (Unload Form2, Set Form2=Nothing)
>This works fine. The form AND the private variable are destroyed.
>Can anyone tell me if this is a "design feature" and if so why?