Method for how to operate other forms of controls or variables!
There is a lot of ways to solve, and only simple summary here is here. For other methods, please advise!
1. Define global variables
In fact, this method is simple and very well understood, we can define two global variables in a Module.
For example: public fm1 as new form1 ()
Public FRM2 AS New Form2 ()
In this way, we are easy to visit where you want to visit. However, this will consume more system resources. Generally, don't use global variables.
2. Use Shared prefix
Fields (variables) or properties defined by Shared can be called by other forms.
For example: (assuming forms of forms1 and form2)
Write in Form1:
Public shared m_add as string 'This way we can change the value of M_ADD anywhere in Form1
Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.Load
m_add = "111"
End Sub
Write in Form2:
Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click
Messagebox.show (form1.m_add) 'Obviously we can get this variable of Form1
End Sub
Note: When using Shared, no instance is defined, the variable or attribute defined as Shared is a value in all instances. Like the example in the example above, use Form1.m_ADD. So if it is an instance of opening a lot of the same form, the value does not change, can be used to record the number of instances opened.
3. This method passes a form example in which the variables and attributes of this instance can be obtained. Of course, they can also operate them.
For example: in Form1
DIM FRM AS New Form2 (ME)
frm.show ()
In Form2:
DIM M_PARENT AS FORM1
Public Sub New (Byval Sender As Object)
Mybase.new ()
'This call is required for the Windows Form Designer.
InitializeComponent ()
M_parent = ctype (Sender, Form1)
'Add any initialization after INITIALIZECOMPONENT ()
End Sub
Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click
m_parent.textBox1.text = "1111"
End Sub
Note: This method can be very convenient to obtain and use another form of variables and controls, is a good choice. If we just want to get some variables or attributes from Form1, you can transfer variables or properties in the past, not an example of a form.