Interactions between. NET forms

Source: Internet
Author: User
Tags variables requires valid
When interacting with. NET forms programming, you should firmly grasp the following principles: Before you access a form, you must instantiate it, and if multiple code in your project accesses the same form, you must pass the same instance pointer to the code. This is a serious challenge for a Visual Basic 6.0 programmer who has long been accustomed to using the default form instance as a global variable directly. The good news is that. NET provides you with two options: First, save the form instance pointer in a global variable, and second, pass the form instance pointer to any form, class, module, or procedure that requires access to it.
Numerical Global in. NET
Visual Basic. NET does not support global variables, but it can simulate global variables with the help of Shared (equivalent to Static in C #) variables. In fact, the Definstance property that is automatically added to the form code by the Visual Basic Upgrade Wizard described earlier is a Shared class member. Whether a form class that accommodates the Definstance property has been instantiated, it can be referenced by any code in the project. Does a Shared property like this not correspond to a global variable? So, 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
You need to save an instance of the form to a class when you first instantiate a form:
Dim Mynewcust as New customerform ()
Mynewcust.show ()
Myforms.customerform = Mynewcust
The CustomerForm property value here is your form instance. As a result, 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
Saving the form instance as a property value in this way simulates the global variable in Visual Basic 6.0, as you would expect. The "global variable" that is so simulated has a higher level of scope than class scope. A class domain is a variable that is valid only in the class that defines it (specifically, the module, class, or form). One level lower than the class domain is the process domain (procedure scope), in which the variable is only valid in the routines that define it.
How the form pointer is passed through the project
In addition to the form instance being global, you can also save the form class pointer in a variable and pass it to a routine that requires access to the form. Suppose you have a form Form1, and you want to open a second form Form2 when you click on a button (Button1) in Form1, and then do a calculation when you click another button (FORM2) in the second form Button2. You can write the entire code in Form1, that is:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim MyForm2 as Form2

Private Sub button1_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
Whether the form pointer is global or it is passed as a parameter, it is feasible. However, you must choose the best solution according to the needs of the project. When only a few procedures in a. NET project require access to a particular form, I recommend that you add an argument to these procedures to accept the form pointer if necessary. When your project has too many processes to access the form, you should consider setting a global form pointer variable. Of course, you'd better consider adjusting your project code structure so that there is only one class or procedure that actually accesses the form. If you want to use a form to display login information, you can create a class, save the form instance as its Shared class member, and then add a shared method Writetologwindow to complete the actual form access. As a result, any code in the project can indirectly access the form that displays the login information simply by calling this Writetologwindow method:
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.txtLogInfo.Text)
Sb. Append (Environment.NewLine)
Sb. Append (Message)
M_LogForm.txtLogInfo.Text = sb. ToString ()
End Sub
End Class
Read and change information in the body of the window
So far, we've been talking about creating and accessing form instances without involving how to read or change the information in the body of the window. If your form has been instantiated according to the previous method, and the code that accesses the form is in the same project as the form, you can directly manipulate any of the controls in the form to read and change the information in the window. But I think this is not ideal. Instead of directly accessing controls such as text boxes, buttons, and so on in a form, you might as well add a public property that controls the controls on the form. If you are interested in trying this particular form of access, follow me:
Create a new Windows application project in Visual Basic. NET.
A form Form1 has been generated automatically at this time in the project. Now add another form Form2: Right-click the project name-> add-> Add Windows form in Solution Explorer-> Click Open to accept the default name Form2.vb.
Add two buttons to the Form1, named Button1 and Button2 by default values, and adjust their position in the form to avoid overlapping.
Add a simple text box to the Form2, named TextBox1 by default
Add the following code to the Form2 "End Class" (In Solution Explorer, right-click "Form2"-> "View Code", and 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
The next thing to do is:
A. Switch to Form1 code and add a line after "Inherits System.Windows.Forms.Form":
Dim MyForm2 as New Form2 ()
B. In Form1, double-click the Button1 button to enter the following code in its Click event handler code:
Myform2.customername = "Fred"
Myform2.show ()
C. In Form1, double-click the Button2 button and enter the following code in its Click event handler code:
MessageBox.Show (Myform2.customername)
Myform2.customername = "Joe"
D. Run the project by F5 and click on the Button1 and Button2 buttons in the form to see how the code works.
On the face of it, accessing the Form2 via the CustomerName property is very similar to the direct access Form2. However, this indirect form access can bring a lot of benefits, the most important thing is that it achieve a higher abstraction. In other words, you can exchange data with Form2 even if you don't know any details of the controls in the Form2 (such as whether the form contains a TextBox control); All you have to do is read or set the CustomerName property value. With this abstraction, you can modify the FORM2 implementation without affecting other code in the project, thus greatly simplifying the maintenance of the entire project code. From the example of this article, this type of property-based form programming pattern does not seem to be simpler than the conventional approach. However, it hides all the details of the form as a property, so you can access the form with concise, consistent code. So, it can be a great help in some fairly complex user interface programming. All in all, the programming pattern of accessing forms and their controls through attribute values is valuable to programmers: it is not only more professional than the programming patterns that direct access to forms, but also makes the code for the entire project readable and legible.

In the project I used the method of global variables, 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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.