| Visual Basic forms have two sides: a visual interface and a code module. The visual display can not exist if the corresponding code is not in memory but it is possible for the code module to be in memory when the visual display has been unloaded. The easiest way to visualize the process is to picture the VB design environment. The visual form is shown in one window on which you place controls and set the various properties of the form and the controls. A second window contains the code associated with the form. In that window you define module-level and procedure-level variables as well as the actual procedures. In order for code in another module to reference subroutines, functions, properties or variables associated with a form the code portion must be in memory. The first time any of these elements is referenced the code segment is brought into memory. As long as no reference is made to any element that is contained in the visual interface portion of the form the visual half is not loaded. The following code snippets load just the code segment: Dim x As Form1 Set x=New Form1 x.MyProperty=2 ' call public property on the form x.MyMethod ' run public sub on the form MsgBox x.MyFunction(3) ' call public function When the code segment is first loaded into memory the Form_Init routine is run. This happens no matter what triggered the load and allows the programmer a chance to initialize form-level variables properly. The visual portion of the form is loaded into memory when any of the visual components are referenced in any way, or when the Load statement is used. If the code segment is not already in memory it will be loaded first (and the Form_Init will run as part of that process). When the visual portion is instantiated the Form_Load event will run. This gives the programmer the chance to initialize controls and/or visual form properties. Loading the visual interface also adds an element to the application's Forms collection. Removing a form form memory is also accomplished in two distinct phases. The visual interface is removed from the Forms collection and from memory by executing the Unload command on the reference as in: Unload Form2 or Dim xForm As Form For Each xForm in Forms Unload xForm Next These statements will trigger the Form_QueryUnload and then the Form_Unload events allowing the programmer the chance to clean up as necessary before the visual interface is removed from memory. If no object variable still holds a reference to the form the Form_Terminate event fires and the code segment is flushed from memory as well. If any reference does still exist then it must be explicitly released with code such as: Set Form2=Nothing Set Form1=Nothing When the form has been Unloaded so that the visual interface and it's link to the code segment are gone, and the last object reference to the form is set =Nothing then Form_Terminate fires and the form is gone from memory. Note the following sequence: Form2.Show ' showing visual segment requires loading code first... Form2.MyVariable=2 ' set public variable on form Form2.Text1.Text="My Text" ' set contents of textbox Unload Form2 ' remove visual segment Debug.Print Form2.MyVariable ' will show 2 Debug.Print Form2.Text1.Text ' will run Form_Load and print "Text1" (or whatever is set in design mode for Text1 on Form2) Unloading a form releases the visual interface but NOT the code segment. The module-level variables, such as 'MyVariable' in the above example, remain in memory as part of the code segment and are still present when the form reloads. Adding 'Set Form2=Nothing' after the Unload statement would flush that and printing the variable would then load a new copy of the code segment, run the Form_Init event, and display the default value. One last note: The code segment is actually itself in two parts. It has a code portion consisting of the procedural instructions and a data portion containing the current variable values. The code portion is shared among all instances of the form in memory and remains as long as any instance exists. The data portion is unique to each instance: Dim frmDemo1 As Form2 Dim frmDemo2 As Form2 Set frmDemo1=New Form2 Set frmDemo2=New Form2 frmDemo1.Show ' load visual and code segments frmDemo2.Show ' load second visual, second copy of variables ' there is actually only one copy of the code in memory Unload frmDemo1 ' remove one visual segment Set frmDemo1=Nothing ' remove it's data, leave the code Unload frmDemo2 ' remove second visual segment Set frmDemo2=Nothing ' flush data & code pieces |
Instantiating/Unloading Forms |
Express News India | Freelance ecommerce web development India