How to: Access a Form
You can access the members of a Visual Basic form without having to create a variable. The following examples demonstrate this by changing colors on a form.
Accessing a Form
To access Form1
Make sure your project has a reference to the System.Drawing namespace. This is necessary to work with the color settings, not to access the form.
Change the colors directly on
Form1
.Call the Show method directly on
Form1
.Public Sub ChangeForm1Colors() Form1.ForeColor = System.Drawing.Color.Coral Form1.BackColor = System.Drawing.Color.Cyan Form1.Show() End Sub
If
Form1
does not already exist, Visual Basic creates it for you. You do not have to declare a variable for it.
Creating an Additional Instance of a Form
If you want to create a new form, rather than access an existing one, you can declare a variable and initialize it using the New keyword.
To create an additional copy of Form1
Make sure your project has a reference to the System.Drawing namespace. This is necessary to work with the color settings, not to access the form.
Assign
New Form1
to a variable.Public Sub GetSecondInstance() Dim newForm1 As New Form1 newForm1.BackColor = System.Drawing.Color.YellowGreen newForm1.Show() End Sub
If you want to display two or more copies of the same form, you must create the additional copies. The preceding example creates a second copy of
Form1
and paints it a different color. You can then access the original copy usingForm1
and the second copy usingnewForm1
.