VB .Net

Multiple Forms

Last revised: December 3, 2003

You may have more than one forms in an application. For example, you may have a main form (e.g. frmMain) as a startup form and another form that displays a message (e.g. frmMessage) in an application.

There are three aspects associated with multiple forms:

To follow the instruction with an example below, create a new Windows application in VB .Net. Then change the default form name to frmMain in the Solution Explorer and Properties windows as shown below:

Solution Explorer Window

Adding an additional form:

  1. From the main menu bar, click on File --> Add New Item.
  2. In the Add New Item window, select Windows Form template, then type frmMessage.vb. Click Open.
  3. You will see frmMessage form in the Solution Explorer window.

***********************************************************************

Displaying the form

To display a form, you need to create a representation of the form. It is called instantiation. When you create a new form as shown above, you create a class. It is a sort of a blue print without substance. Instantiation of a class creates an object based on the blue print. To manipulate the form's substance, you need to instantiate or create a representation of the form.

  1. Add a command button in the frmMain.
  2. Add the following codes in the command button procedure:

    Private Sub btnDisplayMessageForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMessageForm.Click

              Dim MessageForm As New frmMessage()

    End Sub

    The Dim statement instantiates or makes an object representing the frmMessage form. You will use MessageForm to refer to the frmMessage form.

  3. Before discussing displaying the MessageForm form, let's talk about hiding the main form (frmMain). Often times, you may want to hide the current form when you open anther form. Here is how:

    After the Dim statement, add the following code that will hide the main form from the screen:

              Me.Hide( )

    Here, Me is a instance or a reference to the current form. In this case, it is referring to frmMain form. The Hide method simply hides the form from the user, but it continues to execute the codes in the form.
  4. There are two different ways to display another form. One is using the ShowDialog method (displaying a modal form).

    After the Me.Hide( ) statement, add:

              MessageForm.ShowDialog( )

    The ShowDialog method will display frmMessage on the monitor and discontinue to execute the codes after the statement until the frmMessage form is closed. It means that if you had codes after MessageForm.ShowDialog( ), they will not be executed or wait until the MessageForm form is closed.

    Another way of displaying the message form is using the Show method (displaying a modeless form). See the details.
  5. At the end, the entire codes of the command button procedure will be:
    Private Sub btnDisplayMessageForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMessageForm.Click
              Dim MessageForm As New frmMessage( )
              Me.Hide( )
              MessageForm.ShowDialog( )
              ' more codes to be executed ONLY after the frmMessage form is closed.
              ' you may add more codes later.
              '

    End Sub
  6. Before start debugging (F5), set the startup object. First, click on the Project menu on the main menu bar, then click on Properties option. Then select frmMain as the startup object. Click OK.
  7. Press the F5 key to start debugging.
  8. In the frmMain form, click on the button.

    Then the frmMain form will disappear (Hide) and the frmMessage form will appear (ShowDialog) as below:

    There is no message yet. Move on for now.
  9. To stop debugging, press Shift + F5 or click on the Debug menu on the main menu bar, then click on Stop Debugging option.

Displaying a modeless form:

After the Dim statement, replace :
          MessageForm.ShowDialog( )
with
          MessageForm.Show( )
The Show method will display frmMessage on the monitor and it will continuously execute the codes after the statement regardless whether the message form is closed or not.

At the end, your codes will be as following:

Private Sub btnDisplayMessageForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayMessageForm.Click
          Dim MessageForm As New frmMessage( )
          Me.Hide( )
          MessageForm.Show( )
          ' more codes to be executed AS SOON AS the frmMessage form is shown.
          ' and codes will NOT wait for the frmMessage form to closed.
          '

End Sub

 

***********************************************************************

Accessing data from another form

  1. If you have a modeless form method, change it back to a modal form method. It should be:

              MessageForm.ShowDialog( )
  2. You can access information from another form as long as they are not closed (hiding is okay). On the frmMessage form, add a label box named lblMessage and a command button named btnDisplayMessage.
  3. Add the following code in the command button procedure as:

              Dim MainForm As New frmMain( )

    Here, MainForm is an instantiation or representation of the frmMain class. You will use MainForm to refer to frmMain in codes.
  4. The following codes is an example of getting a datum from a control from the main form.
    After the Dim statement add the following code:

              lblMessage.Text = MainForm.btnDisplayMessageForm.Text

    This codes will get the text property value of the button in the main form and it will assign it to the message label in the message form.
  5. At the end, the entire codes will look as below:
    Private Sub btnDisplayMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
              Dim MainForm As New frmMain()

              lblMessage.Text = MainForm.btnDisplayMessageForm.Text
    End Sub

  6. Debug the application (F5). You will see:

    Click the button. Then the main form will hide and the message form will appear.

    Click on the button. It will display the text value of the buttom in the main form in the message label in the message form.


    Close the form, then press Shift + F5 or click on the Debug menu on the main menu bar, then click on Stop Debugging option.
  7. You can access values of variables in another form in the same way. However, the source variable has to be a Public variable. For example, add the following code in the global section of the main form:

    Public strMessage As String = "Have a great day!"

    So, the codes window will look as :
    Public Class frmMain
              Inherits System.Windows.Forms.Form
              Public strMessage As String = "Have a great day!"
    strMessage is a public global variable with an initial value of "Have a great day!".
  8. In the message form, replace
              lblMessage.Text = MainForm.btnDisplayMessageForm.Text
    with
              lblMessage.Text = MainForm.strMessage

    So, the entire codes of the procedure will be:
    Private Sub btnDisplayMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
              Dim MainForm As New frmMain()

              lblMessage.Text = MainForm.strMessage
    End Sub

    The basic format of refering to a variable in another form is
               FormInstanceName.VariableName
  9. If you run the debug, you will have:

    Close the window and application (Shift + F5).
  10. Now, add another command button named btnReturn in the message form.
  11. Add the following code in the btnReturn procedure:

    Private Sub btnReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturn.Click
              Dim MainForm As New frmMain( )
              MainForm.Show( )
              Me.Close( )

    End Sub

    Here, Dim MainForm As New frmMain( ) will instantiate the frmMain, and MainForm.Show( ) will display the main form and continue to execute the follow up codes that is Me.Close( ) which in turn will close the message form.

Here is a zip file of the project.