Microsoft KB Archive/841292

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

Article ID: 841292

Article Last Modified on 11/27/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





IN THIS TASK

 * INTRODUCTION
 * Requirements
 * Method 1: Declare an object reference in the Form2 Windows Form
 * Create the Form1 Windows Form
 * Create the Form2 Windows Form
 * Add code in the Form1 Windows Form to display the Form2 Windows Form
 * Build and then run the project
 * Method 2: Create an instance of the Form2 Windows Form in the Form1 Windows Form
 * Create the Form1 Windows Form
 * Create the Form2 Windows Form
 * Add code in the Form1 Windows Form to display the Form2 Windows Form
 * Build and then run the project
 * REFERENCES



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.
 * 1) Under Project Types, click Visual Basic Projects.

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

By default, the Form1 Windows Form is created.
 * 1) On the View menu, click Toolbox.
 * 2) Add two Label controls to the Form1 Windows Form.

Note The two Label controls must be next to each other.
 * 1) 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.
 * 1) On the View menu, click Properties Window.
 * 2) Set the Text property of the Label1 Label control to the following:

Value set by Form2 =
 * 1) Set the Size property of the Label1 Label control to the following:

115, 15
 * 1) Delete the text in the Text property of the Label2 Label control.
 * 2) Set the Size property of the Label2 Label control to the following:

100, 15
 * 1) Set the Text property of the Button1 Button control to the following:

Display Form2
 * 1) Set the Size property of the Button1 Button control to the following:

85, 25
 * 1) On the File menu, click Save Form1.vb to save the file.

back to the top

Create the Form2 Windows Form
 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. Under Templates, click Windows Form, and then click Open.

By default, the Form2 Windows Form is created. Add a TextBox control to the Form2 Windows Form. 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. In the Properties window, delete the text in the Text property of the TextBox1 TextBox control. Set the Text property of the Button1 Button control to the following:

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

215, 20</li> In Solution Explorer, right-click Form2.vb, and then click View Code.</li>  In the Form2.vb file, locate the following code: Public Class Form2 Inherits System.Windows.Forms.Form </li>  Add the following code after the code that you located in step 9: ' Declare an object reference. Public objForm As Object </li>  In the Form2.vb file, locate the following code: End Class </li>  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 </li> On the File menu, click Save Form2.vb to save the file.</li></ol>

back to the top

Add code in the Form1 Windows Form to display the Form2 Windows Form
<ol> In Solution Explorer, right-click Form1.vb, and then click View Code.</li>  In the Form1.vb file, locate the following code: End Class </li>  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 </li> On the File menu, click Save Form1.vb to save the file.</li></ol>

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.
 * 1) Click Display Form2.

The Form2 Windows Form appears.
 * 1) 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.
 * 2) Switch to the Form1 Windows Form.
 * 3) 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.
 * 1) Under Project Types, click Visual Basic Projects.

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

By default, the Form1 Windows Form is created.
 * 1) On the View menu, click Toolbox.
 * 2) Add two Label controls to the Form1 Windows Form.

Note The two Label controls must be next to each other.
 * 1) 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.
 * 1) On the View menu, click Properties Window.
 * 2) Set the Text property of the Label1 Label control to the following:

Value set in Form2 =
 * 1) Set the Size property of the Label1 Label control to the following:

115, 15
 * 1) Delete the text in the Text property of the Label2 Label control.
 * 2) Set the Size property of the Label2 Label control to the following:

100, 15
 * 1) Set the Text property of the Button1 Button control to the following:

Display Form2
 * 1) Set the Size property of the Button1 Button control to the following:

85, 25
 * 1) On the File menu, click Save Form1.vb to save the file.

back to the top

Create the Form2 Windows Form
<ol> 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.</li> Under Templates, click Windows Form, and then click Open.

By default, the Form2 Windows Form is created.</li> Add a TextBox control to the Form2 Windows Form.</li> 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.</li> In the Properties window, delete the text in the Text property of the TextBox1 TextBox control.</li> <li>Set the Text property of the Button1 Button control to the following:

Set the Form2Value property</li> <li>Set the Size property of the Button1 Button control to the following:

160, 25</li> <li>In Solution Explorer, right-click Form2.vb, and then click View Code.</li> <li> In the Form2.vb file, locate the following code: Public Class Form2 Inherits System.Windows.Forms.Form </li> <li> Add the following code after the code that you located in step 9: Private MyVal As String </li> <li> In the Form2.vb file, locate the following code: End Class </li> <li> 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 </li> <li>On the File menu, click Save Form2.vb to save the file.</li></ol>

back to the top

Add code in the Form1 Windows Form to display the Form2 Windows Form
<ol> <li>In Solution Explorer, right-click Form1.vb, and then click View Code.</li> <li> In the Form1.vb file, locate the following code: End Class </li> <li> 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 </li> <li>On the File menu, click Save Form1.vb to save the file.</li></ol>

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.
 * 1) Click Display Form2.

The Form2 Windows Form appears.
 * 1) Type some text in the box that you see on the Form2 Windows Form, and then click Set the Form2Value property.
 * 2) Close the Form2 Windows Form.
 * 3) Verify that the Label2 Label control contains the text that you typed in the Form2 Windows Form.

back to the top

<div class="references_section">