ecommerce web developer development freelance website designer India
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

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200

Express News India | Freelance ecommerce web development India