Interaction between. NET forms

Source: Internet
Author: User
. 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.

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.