Microsoft KB Archive/832680

= How to call a subroutine in a Windows Form class from another Windows Form class =

Article ID: 832680

Article Last Modified on 11/27/2007

-

APPLIES TO


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

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Step-by-step sample
 * Create the Form1 Windows Form
 * Create the Form2 Windows Form
 * Build the solution and then run the solution
 * REFERENCES



SUMMARY
This step-by-step article describes how to call a subroutine in a Windows Form class from another Windows Form class.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 programming

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 or Microsoft Windows XP
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

back to the top

Step-by-step sample
To call a subroutine in a Windows Form class from another Windows Form class, make the subroutine public. Next, in the Windows Form class that is calling, create an instance of the Windows Form class that contains the subroutine. Visual Basic .NET or Visual Basic 2005 supports object-oriented programming. Therefore, to call a subroutine or to call a method, you must create an instance of the class that contains the subroutine or that contains the method.

The following steps describe how to call an Add subroutine in the Form1 Windows Form class from the Form2 Windows Form class.

back to the top

Create the Form1 Windows Form
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. Under Templates, click Windows Application. Type WindowsApplication1 in the Name box, and then click OK.

By default, the Form1 Windows Form is created. Add a TextBox control to the Form1 Windows Form.

By default, the TextBox1 control is created. Right-click Form1, 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 8: Public Sub Add(ByVal P As Integer, ByVal Q As Integer) Dim r As Integer = P   Dim l As Integer = Q    Dim n As Integer = (r + l)    TextBox1.Text = n End Sub </li> On the File menu, click Save Form1.vb to save the file.</li></ol>

back to the top

Create the Form2 Windows Form
<ol> In Solution Explorer, right-click the WindowsApplication1 project.</li> Point to Add, and then click Add New Item.

The Add New Item - WindowsApplication1 dialog box appears.

Note In Visual Studio 2005, point to Add, and then click New Item.</li> Under Templates, click Windows Form.</li> In the Name box, type Form2, and then click Open.</li> Add two TextBox controls to the Form2 Windows Form.

By default, the TextBox1 control and the TextBox2 control are created.</li> Add a Button control to the Form2 Windows Form.

By default, the Button1 control is created.</li>  Double-click Button1, and then add the following code to the Button1_Click event handler: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim p As Integer = Int32.Parse(TextBox1.Text) Dim q As Integer = Int32.Parse(TextBox2.Text) Dim frm As New WindowsApplication1.Form1 Dim r As Integer frm.Add(p, q)   frm.Show End Sub </li> On the File menu, click Save Form2.vb to save the file.</li></ol>

back to the top

Build the solution and then run the solution

 * 1) On the Build menu, click Build Solution.
 * 2) In Solution Explorer, right-click the WindowsApplication1 project, and then click Properties.

The WindowsApplication1 Property Pages dialog box appears.
 * 1) In the Startup object box, click to select Form2, and then click OK.
 * 2) On the Debug menu, click Start to run the project.

The Form2 Windows Form appears.
 * 1) In the TextBox1 box, type any number.
 * 2) In the TextBox2 box, type any number, and then click Button1.
 * 3) Verify that the value in the TextBox1 box of the Form1 Windows Form is the sum of the values that you typed in step 5 and in step 6.

back to the top

<div class="references_section">