How to access the members of a Windows Form in another Windows Form by using Visual Basic .NET or Visual Basic 2005 (841292)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

INTRODUCTION

This article describes how to access Public members that are declared in one Windows Form class from another Windows Form class by using Microsoft Visual Basic .NET or Microsoft Visual Basic 2005. To do this, you can use either of the following methods:
  • Method 1: Declare an object reference in the Form2 Windows Form. Create an instance of the Form2 Windows Form in the Form1 Windows Form, and then set the object reference in the Form2 Windows Form to the current instance of the Form1 Windows Form. You can now access all Public members of the Form1 Windows Form from the Form2 Windows Form.
  • Method 2: Create an instance of the Form2 Windows Form in the Form1 Windows Form. You can now access all Public members of the Form2 Windows Form from the Form1 Windows Form.
back to the top

Requirements

This article assumes that you are familiar with the following topics:
  • Programming with Visual Basic .NET or Visual Basic 2005
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
back to the top

Method 1: Declare an object reference in the Form2 Windows Form

To access variables, properties, and methods that are declared in a Windows Form class from another Windows Form class, you must declare the variables, the properties, and the methods as Public members. To access these members, you can create the Form1 Windows Form class and the Form2 Windows Form class, and then declare an object reference in the Form2 Windows Form class. Next, create an instance of the Form2 Windows Form class in the Form1 Windows Form class, and then set the object reference that you declared in the Form2 Windows Form class to the current instance of the Form1 Windows Form class. You can now access all Public members of the Form1 Windows Form from the Form2 Windows Form.

back to the top

Create the Form1 Windows Form

To create the Form1 Windows Form, follow these steps:
  1. Start Visual Studio .NET or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.
  3. Under Project Types, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Under Templates, click Windows Application.
  5. In the Name box, type Method1App, and then click OK.

    By default, the Form1 Windows Form is created.
  6. On the View menu, click Toolbox.
  7. Add two Label controls to the Form1 Windows Form.

    Note The two Label controls must be next to each other.
  8. Add a Button control to the Form1 Windows Form.

    Note The Button control must be under the two Label controls that you added in step 7.
  9. On the View menu, click Properties Window.
  10. Set the Text property of the Label1 Label control to the following:

    Value set by Form2 =
  11. Set the Size property of the Label1 Label control to the following:

    115, 15
  12. Delete the text in the Text property of the Label2 Label control.
  13. Set the Size property of the Label2 Label control to the following:

    100, 15
  14. Set the Text property of the Button1 Button control to the following:

    Display Form2
  15. Set the Size property of the Button1 Button control to the following:

    85, 25
  16. On the File menu, click Save Form1.vb to save the file.
back to the top

Create the Form2 Windows Form

  1. In Solution Explorer, right-click the Method1App project, point to Add, and then click Add New Item.

    The Add New Item - Method1App dialog box appears.
  2. Under Templates, click Windows Form, and then click Open.

    By default, the Form2 Windows Form is created.
  3. Add a TextBox control to the Form2 Windows Form.
  4. Add a Button control to the Form2 Windows Form.

    Note The Button control must be under the TextBox control that you added in step 3.
  5. In the Properties window, delete the text in the Text property of the TextBox1 TextBox control.
  6. Set the Text property of the Button1 Button control to the following:

    Set the Text property of Label2 in Form1
  7. Set the Size property of the Button1 Button control to the following:

    215, 20
  8. In Solution Explorer, right-click Form2.vb, and then click View Code.
  9. In the Form2.vb file, locate the following code:
    Public Class Form2
        Inherits System.Windows.Forms.Form
  10. Add the following code after the code that you located in step 9:
       ' Declare an object reference.
       Public objForm As Object
  11. In the Form2.vb file, locate the following code:
    End Class
  12. Add the following code before the code that you located in step 11:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          ' Access the Text property of the Label2 Label control.
          CType(objForm, Form1).Label2.Text = TextBox1.Text
       End Sub
  13. On the File menu, click Save Form2.vb to save the file.
back to the top

Add code in the Form1 Windows Form to display the Form2 Windows Form

  1. In Solution Explorer, right-click Form1.vb, and then click View Code.
  2. In the Form1.vb file, locate the following code:
    End Class
  3. Add the following code before the code that you located in step 2:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          ' Create an instance of the Form2 Windows Form.
          Dim MyForm As New Form2()
          ' Set the object reference that you declared in the
          ' Form2 Windows Form to the current instance of Form1.
          MyForm.objForm = Me
          MyForm.SetBounds(400, 400, 300, 300)
          ' Display the Form2 Windows Form.
          MyForm.Show()
       End Sub
  4. On the File menu, click Save Form1.vb to save the file.
back to the top

Build and then run the project

  1. On the Build menu, click Build Method1App.
  2. On the Debug menu, click Start to run the project.

    The Form1 Windows Form appears.
  3. Click Display Form2.

    The Form2 Windows Form appears.
  4. Type some text in the box that you see on the Form2 Windows Form, and then click Set the Text property of Label2 in Form1.
  5. Switch to the Form1 Windows Form.
  6. Verify that the Label2 Label control contains the text that you typed in the Form2 Windows Form.
back to the top

Method 2: Create an instance of the Form2 Windows Form in the Form1 Windows Form

To access variables, properties, and methods that are declared in a Windows Form class from another Windows Form class, you must declare the variables, the properties, and the methods as Public members. To access these Public members, you can create an instance of the Form2 Windows Form class in the Form1 Windows Form class. You can now access all Public members of the Form2 Windows Form from the Form1 Windows Form.

back to the top

Create the Form1 Windows Form

  1. Start Visual Studio .NET or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.
  3. Under Project Types, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Under Templates, click Windows Application.
  5. In the Name box, type Method2App, and then click OK.

    By default, the Form1 Windows Form is created.
  6. On the View menu, click Toolbox.
  7. Add two Label controls to the Form1 Windows Form.

    Note The two Label controls must be next to each other.
  8. Add a Button control to the Form1 Windows Form.

    Note The Button control must be under the two Label controls that you added in step 7.
  9. On the View menu, click Properties Window.
  10. Set the Text property of the Label1 Label control to the following:

    Value set in Form2 =
  11. Set the Size property of the Label1 Label control to the following:

    115, 15
  12. Delete the text in the Text property of the Label2 Label control.
  13. Set the Size property of the Label2 Label control to the following:

    100, 15
  14. Set the Text property of the Button1 Button control to the following:

    Display Form2
  15. Set the Size property of the Button1 Button control to the following:

    85, 25
  16. On the File menu, click Save Form1.vb to save the file.
back to the top

Create the Form2 Windows Form

  1. In Solution Explorer, right-click the Method2App project, point to Add, and then click Add New Item.

    The Add New Item - Method2App dialog box appears.
  2. Under Templates, click Windows Form, and then click Open.

    By default, the Form2 Windows Form is created.
  3. Add a TextBox control to the Form2 Windows Form.
  4. Add a Button control to the Form2 Windows Form.

    Note The Button control must be under the TextBox control that you added in step 3.
  5. In the Properties window, delete the text in the Text property of the TextBox1 TextBox control.
  6. Set the Text property of the Button1 Button control to the following:

    Set the Form2Value property
  7. Set the Size property of the Button1 Button control to the following:

    160, 25
  8. In Solution Explorer, right-click Form2.vb, and then click View Code.
  9. In the Form2.vb file, locate the following code:
    Public Class Form2
        Inherits System.Windows.Forms.Form
  10. Add the following code after the code that you located in step 9:
    Private MyVal As String
  11. In the Form2.vb file, locate the following code:
    End Class
  12. Add the following code before the code that you located in step 11:
       ' Declare a Public property that you can access in the Form1 Windows Form.
       Public Property Form2Value() As String
          Get
             Return MyVal
          End Get
          Set(ByVal Value As String)
             MyVal = Value
          End Set
       End Property
    
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Me.Form2Value = TextBox1.Text
       End Sub
    
       Private Sub Form2_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed
          ' Set the DialogResult property.
          Me.DialogResult = DialogResult.OK
       End Sub
  13. On the File menu, click Save Form2.vb to save the file.
back to the top

Add code in the Form1 Windows Form to display the Form2 Windows Form

  1. In Solution Explorer, right-click Form1.vb, and then click View Code.
  2. In the Form1.vb file, locate the following code:
    End Class
  3. Add the following code before the code that you located in step 2:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          ' Create an instance of the Form2 Windows Form.
          Dim objForm As New Form2()
          ' Use the ShowDialog method to display the Form2 Windows Form.
          If (objForm.ShowDialog() = DialogResult.OK) Then
             ' Set the Text property of the Label2 Label control to the value
             ' of the Form2Value property of the Form2 Windows Form.
             Label2.Text = objForm.Form2Value
          End If
          ' Release the instance of the Form2 Windows Form.
          objForm = Nothing
       End Sub
  4. On the File menu, click Save Form1.vb to save the file.
back to the top

Build and then run the project

  1. On the Build menu, click Build Method2App.
  2. On the Debug menu, click Start to run the project.

    The Form1 Windows Form appears.
  3. Click Display Form2.

    The Form2 Windows Form appears.
  4. Type some text in the box that you see on the Form2 Windows Form, and then click Set the Form2Value property.
  5. Close the Form2 Windows Form.
  6. Verify that the Label2 Label control contains the text that you typed in the Form2 Windows Form.
back to the top

REFERENCES

For additional information, visit the following Microsoft Developer Network (MSDN) Web sites:

Working with multiple forms in Visual Basic .NET: upgrading to .NETForm classback to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbWindowsForms kbForms kbClient kbHOWTOmaster KB841292 kbAudDeveloper