Share via


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

  1. 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.

  2. Change the colors directly on Form1.

  3. 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

  1. 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.

  2. 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 using Form1 and the second copy using newForm1.

See Also

Reference

New (Visual Basic)
System.Drawing
Form