Forms are graphical objects that make up your applications user interface. Within Visual Basic, classes define how forms are displayed and what they can do. When a form is displayed at runtime, Visual Basic .NET creates an instance of the Form class that you can use like any other object. You can add custom methods and properties to forms and access them from other forms or classes in your application.

To create a new method for a form

  • Add a procedure declared as Public, as in the following code:

    Public Sub PrintMyJob()
    '.
    '.Add code for your method here.
    '.
    End Sub

To add a new field to a form

  • Declare a public variable in the form module, as in the following code:
    Public IDNumber As Integer

To access methods on a different form

  1. Create a new instance of the form whose methods you want to access. When you reference a form name, you are actually referencing the class the form belongs to, not the object itself.

    Note Visual Basic 6 provided, for each form class, an implicit global variable with the same name as the form class. Visual Basic .NET does not provide an implicit variable declaration.

  2. Assign the form to an object variable. The object variable references a new instance of the form class.

    The following example correctly calls the PrintMyJob procedure:

    Dim newForm1 As New Form1
    newForm1.PrintMyJob()

In the previous example, the new form is not displayed. It is not necessary to show a form object to use its methods.