Ways to manipulate controls or variables on other forms
Source: Internet
Author: User
Variables | Controls how to manipulate controls or variables for other forms!
In fact, there are many ways to solve, here only to do a simple summary. For other methods, please give more advice!
1. Defining Global Variables
In fact, this method is relatively simple and very good understanding, we only have a module inside the definition of two global variables can be.
For example: Public Frm1 as New Form1 ()
Public Frm2 as New Form2 ()
In this way, it is easy for us to visit wherever we want. However, doing so consumes more system resources. Generally, do not use global variables.
2. Using the shared prefix
You use shared-defined fields (variables) or properties that can be called by other forms.
For example: (assuming that there are forms Form1 and Form2)
Write in Form1:
Public Shared M_add as String ' so that we can change m_add values anywhere in Form1
Private Sub Form1_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
M_add = "111"
End Sub
Write in Form2:
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
MessageBox.Show (Form1.m_add) ' Obviously we can get this variable for Form1.
End Sub
Note: When you use shared, you do not define an instance, and a variable or property that is defined as shared is a value in all instances. Just like using Form1.m_add in the example above. So if you open many instances of the same form, the value does not change, and you can use to record the number of open instances.
3. This method passes a form instance past, so that the variables and properties of the instance can be obtained. Of course, they can also be manipulated.
For example: in Form1
Dim frm as New Form2 (Me)
frm. Show ()
In the Form2:
Dim M_parent as Form1
Public Sub New (ByVal sender as Object)
MyBase.New ()
' This call is required by the Windows Forms Designer.
InitializeComponent ()
M_parent=ctype (SENDER,FORM1)
' Add any initialization after the InitializeComponent () call
End Sub
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
M_parent.textbox1.text = "1111"
End Sub
Note: This method makes it easy to obtain and use the variables and controls of another form, which is a good choice. If we just want to get some value from the variable or attribute from Form1, we can pass the variable or property directly past, rather than an instance of the form.
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