. Net form programming should firmly grasp the following principles: before accessing the form, you must instantiate the form; If there are multiple codes in the project to access the same form, you must pass the same instance pointer to the code. This is a serious challenge for Visual Basic 6.0 programmers who have become accustomed to using the default form instance as a global variable. Fortunately. NET provides you with two ways: one is to save the form instance pointer in the global variable; the other is to pass the form instance pointer to any form, class, module, or process that needs to be accessed.
Numerical regionalization in. net
Visual Basic. Net does not support global variables, but it can simulate global variables with shared (equivalent to static in C #) variables. In fact, the definstance attribute automatically added to the form code by the preceding Visual Basic Upgrade Wizard is a shared class member. Whether the Form class containing the definstance attribute has been instantiated or not, it can be referenced by any code in the project. Isn't shared attributes like this equivalent to global variables? Therefore, you can create a class like this:
Public class myforms
Private shared m_customerform as customerform
Public shared property customerform () as customerform
Get
Return m_customerform
End get
Set (byval value as customerform)
M_customerform = Value
End set
End Property
End Class
When instantiating a form for the first time, you need to save the form instance to a class:
Dim mynewcust as new customerform ()
Mynewcust. Show ()
Myforms. customerform = mynewcust
The customerform attribute value here is your form instance. So other code can indirectly access your form from anywhere in the project:
Module doingstuffwithforms
Sub doexcitingthings ()
Myforms. customerform. Text = _
Datetime. Now (). tolongtimestring
End sub
End Module
Save the form instance as a property value to simulate the global variables in Visual Basic 6.0 as required. The scope of the simulated global variable is higher than that of the Class scope. The so-called class field means that a variable is valid only in the class that defines it (specifically, it should include a module, class, or form. A lower level than a category is the procedure scope, that is, the variable is only valid in the routine that defines it.
Transfer Form pointer in Project
In addition to externalizing the form instance, you can save the Form class pointer in the variable and pass it to the routine that needs to access the form. Assume that you have a form form1, and you want to open the second form form2 when you click a button (button1) in form1, and then click another button (button2) in the second form form2). You can write the entire code in form1, namely:
Public class form1
Inherits system. Windows. Forms. Form
Dim myform2 as form2
Private sub button#click (byval sender as system. Object ,_
Byval e as system. eventargs) handles button1.click
Myform2 = new form2 ()
Myform2.show ()
End sub
Private sub button2_click (byval sender as system. Object ,_
Byval e as system. eventargs) handles button2.click
Calculations. compoundinterestcalc (myform2)
End sub
End Class
It is feasible to render the form pointer or pass it as a parameter. However, you must select the best solution based on your project needs. When only a few processes in the. Net project need to access a specific form, I suggest you add a parameter to these processes to accept the form pointer if necessary. When too many processes in your project need to access the form, you should consider setting a global form pointer variable. Of course, you 'd better consider adjusting the project code structure so that there is only one class or process that truly accesses the form. If you want to use a form to display logon information, you can first create a class and save the form instance as its shared class member, then add a shared method writetologwindow to complete the actual form access. Therefore, any code in the project only needs to call the writetologwindow method to indirectly access the form that displays the login information:
Public class log
Private shared m_logform as form2
Public shared property logform () as form2
Get
Return m_logform
End get
Set (byval value as form2)
M_logform = Value
End set
End Property
Public shared sub writetologwindow (byval message as string)
Dim Sb as new _
Stringbuilder(m_logform.txt loginfo. Text)
SB. append (environment. newline)
SB. append (Message)
M_logform.txtloginfo.text = sb. tostring ()
End sub
End Class
Read and change information in the form
So far, we have discussed how to create and access a form instance, but not how to read or change information in the form. If your form has been instantiated according to the preceding method and the code used to access the form is located in the project where the form is located, you can directly operate on any controls in the form to read and change the information in the form. But I think this is not ideal. Instead of directly accessing the text box, buttons, and other controls in the form, it is better to add a public attribute to control the controls in the form. If you are interested in trying this special form access method, please come with me:
Create a new windows application project in Visual Basic. net.
In this case, a form form1 is automatically generated in the project. Now add another form form2: in Solution Explorer, right-click the project name and choose add from the shortcut menu to add a Windows form. Then, click open to accept the default name form2.vb.
Add two buttons in form1, named button1 and button2 by default respectively, and adjust their positions in the form to avoid overlap.
Add a simple text box in form2 and name it textbox1 by default.
Add the following code to the "End Class" of form2 (in Solution Explorer, right-click "form2"-> "view code", and then paste the following code ):
Public property customername () as string
Get
Return textbox1.text
End get
Set (byval value as string)
Textbox1.text = Value
End set
End Property
Next, we will do the following:
A. Switch to the form1 code and add a line after "inherits system. Windows. Forms. Form:
Dim myform2 as new form2 ()
B. Double-click the button1 button in form1 and enter the following code in its Click Event Handler code:
Myform2.customername = "Fred"
Myform2.show ()
C. Double-click the button2 button in form1 and enter the following code in its Click Event Handler code:
MessageBox. Show (myform2.customername)
Myform2.customername = "Joe"
D. Press F5 to run the project and click the buttons button1 and button2 in the form to observe the Code running status.
On the surface, access to form2 through the customername attribute is very similar to direct access to form2. However, this indirect form access method can bring many benefits. The most important thing is that it achieves higher abstraction. In other words, you can exchange data with form2 even if you do not know any details of the control in form2 (for example, whether the form contains a Textbox Control; all you have to do is read or set the customername attribute value. With this abstraction, you can modify the implementation of form2 without affecting other code in the project, thus greatly simplifying the maintenance of the entire project code. From the example in this article, this attribute-based form programming mode does not seem easier than the conventional method. However, it hides all the details of the form in the form of attributes, so it can access the form with concise and consistent code. Therefore, it can be used in some very complex User Interface Programming. All in all, although the programming mode for accessing the form and its controls through attribute values is not intuitive, It is very valuable to programmers: it is not only more professional than the programming mode for directly accessing the form, it also makes the code of the entire project clear and easy to read.
In the project, I use the global variable method. The Code is as follows:
Public class myform
Private shared m_mainform as system. Windows. Forms. Form
Public shared property mainform () as system. Windows. Forms. Form
Get
Return m_mainform
End get
Set (byval value as system. Windows. Forms. Form)
M_mainform = Value
End set
End Property
End Class