How to call a subroutine in a Windows Form class from another Windows Form class (832680)
The information in this article applies to:
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual Basic 2005
SUMMARYThis step-by-step article describes how to call a subroutine
in a Windows Form class from another Windows Form class. back to the topRequirementsThis
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 topStep-by-step
sampleTo 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 topCreate
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.
- In the Form1.vb file, locate the following code:
End Class - 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 - On the File menu, click Save Form1.vb to save the file.
back to the topCreate
the Form2 Windows Form- In Solution Explorer, right-click the
WindowsApplication1 project.
- 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. - Under Templates, click Windows
Form.
- In the Name box, type
Form2, and then click Open.
- Add two TextBox controls to the Form2 Windows Form.
By
default, the TextBox1 control and the TextBox2 control are created. - Add a Button control to the Form2 Windows Form.
By default,
the Button1 control is created. - 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 - On the File menu, click Save Form2.vb to save the file.
back to the
topBuild the solution and then run the
solution- On the Build menu, click Build
Solution.
- In Solution Explorer, right-click the
WindowsApplication1 project, and then click
Properties.
The WindowsApplication1 Property
Pages dialog box appears. - In the Startup object box, click to select
Form2, and then click OK.
- On the Debug menu, click
Start to run the project.
The Form2 Windows
Form appears. - In the TextBox1 box, type any number.
- In the TextBox2 box, type any number, and then click Button1.
- 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
topREFERENCESFor more information about Windows Forms, visit the
following Microsoft Developer Network (MSDN) Web sites: back to the top
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbcode kbAppDev kbUser kbHOWTOmaster KB832680 kbAudDeveloper |
---|
|