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

Article translations Article translations
Article ID: 832680 - View products that this article applies to.
Expand all | Collapse all

On This Page

SUMMARY

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

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

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.

Create the Form1 Windows Form

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  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. Type WindowsApplication1 in the Name box, and then click OK.

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

    By default, the TextBox1 control is created.
  7. Right-click Form1, and then click View Code.
  8. In the Form1.vb file, locate the following code:
    End Class
  9. 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
  10. On the File menu, click Save Form1.vb to save the file.

Create the Form2 Windows Form

  1. In Solution Explorer, right-click the WindowsApplication1 project.
  2. 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.
  3. Under Templates, click Windows Form.
  4. In the Name box, type Form2, and then click Open.
  5. Add two TextBox controls to the Form2 Windows Form.

    By default, the TextBox1 control and the TextBox2 control are created.
  6. Add a Button control to the Form2 Windows Form.

    By default, the Button1 control is created.
  7. 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
  8. On the File menu, click Save Form2.vb to save the file.

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.
  3. In the Startup object box, click to select Form2, and then click OK.
  4. On the Debug menu, click Start to run the project.

    The Form2 Windows Form appears.
  5. In the TextBox1 box, type any number.
  6. In the TextBox2 box, type any number, and then click Button1.
  7. 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.

REFERENCES

For more information about Windows Forms, visit the following Microsoft Developer Network (MSDN) Web sites:
Working with Multiple Forms in Visual Basic .NET: Upgrading to .NET
http://msdn2.microsoft.com/en-us/library/aa289529(VS.71).aspx
Walkthrough: Collecting Data Using a Windows Form
http://msdn2.microsoft.com/en-us/library/f1wckde0(VS.80).aspx

Properties

Article ID: 832680 - Last Review: November 27, 2007 - Revision: 2.5
APPLIES TO
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic 2005
Keywords: 
kbvs2005swept kbvs2005applies kbcode kbappdev kbuser kbhowtomaster KB832680

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com