Building Windows Applications

Working with Multiple Forms in VB .NET

The previous section described a major change in the forms model from VB6 to VB .NET—the removal of default form instances. This is not a major technical problem; programmers (including VB6 programmers) have been working without default instances of non-form classes for a very long time, but it is a big change if you are used to building Windows applications using VB6. The result of this change is that you need a reference to a particular instance of a form to be able to use it. I will start with a very simple example to illustrate the concept. I will create a new Windows application that contains two forms. Form1 , which will be shown automatically when the project runs, will create and display an instance of Form2 when you click a button. To illustrate communication from one form to another, Form2 will also have a button, and it will change the caption of Form1 whenever it is clicked. To get things started, create the new project and add a new form.

Select File, New, Project from the main menu in Visual Studio .NET, and then pick a Visual Basic Windows Application to create. A form will be created with a default name of Form1 . Add a second form by right-clicking the project and selecting Add, Add Windows Form from the menu that appears. Accept the default name of Form2 for the new form and click the Open button to finish the wizard.

Creating and Displaying an Instance of a Form

The first step is to add code to Form1 to create and display an instance of Form2 . Add a button to Form1 , leaving it with a default name of Button1 . Now, double-click the button to enter its event procedure for the Click event. If you used the code shown in Listing 3.19, a new form would open every time you click the button, which is likely not the desired result.

Listing 3.19 Simple Code for Displaying a Form

Private Sub Button1_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button1.Click
  Dim myForm As New Form2
  myForm.Show()
End Sub

Instead, you will move the Form variable to a module level value, and then determine if it already exists in the Click event (Listing 3.20). Then, you will create the form if it does not already exist and show it either way.

Listing 3.20 Code That Will Create and Display Only One Copy of a Form

Dim myForm As Form2
Private Sub Button1_Click( _
  ByVal sender As System.Object, _
  ByVal e As System.EventArgs) _
    Handles Button1.Click
  If myForm Is Nothing Then
    myForm = New Form2
  End If
  myForm.Show()
End Sub

Using the myForm variable allows you to hang on to a reference to your newly created form. Hanging onto the reference returned from creating a new form is useful so that you can talk to this second form if need be. The main reason for using this variable though, is so that you can create and track a single instance of Form2 , instead of creating a new one on every button click. Now, let's make Form2 talk back to Form1 .

Communicating Between Two Forms

If you want Form2 to be able to communicate with Form1 , you need to supply a reference to Form1 . Once you do this, you will be set up for two-way communication, as both forms will be holding a reference to the other. The simplest way to accomplish this is to add a public variable (of type Form1 ) to Form2 , like this:

Public Class Form2
  Inherits System.Windows.Forms.Form
  Public myCaller As Form1

Then, right after you create an instance of Form2 in the button click event (see Listing 3.21), you can set this property.

Listing 3.21 You Can Pass a Form Reference with a Property

Dim myForm As Form2
Private Sub Button1_Click( _
  ByVal sender As System.Object, _
  ByVal e As System.EventArgs) _
  Handles Button1.Click
  If myForm Is Nothing Then
    myForm = New Form2
    myForm.myCaller = Me
  End If
  myForm.Show()
End Sub

If code in Form2 needs to access Form1 , it can now do so through the myCaller variable. Add a button to Form2 and put this code into it, as shown in Listing 3.22.

Listing 3.22 Now that Form2 Has a Reference to Form1 , it Can Access Form1 's Properties

Private Sub Button1_Click( _
  ByVal sender As System.Object, _
  ByVal e As System.EventArgs) _
  Handles Button1.Click
  If Not myCaller Is Nothing Then
    myCaller.Text = Now.ToLongTimeString
  End If
End Sub

Clicking the button on Form1 will create an instance of Form2 and populate Form2 's myCaller variable with a reference to Form1 . Clicking the button on Form2 will access Form1 through the myCaller variable (if it was set) and change its window title. This was a very simple example of communicating between multiple forms, but there will be additional examples as part of the sample applications in Chapters 4 and 5. The next section covers creating and using a form as a dialog box.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski