Article ID: 914354
Article Last Modified on 11/22/2007
APPLIES TO
- Microsoft Visual Basic 2005
- Microsoft Visual Studio 2005 Express Edition
- Microsoft Visual Studio 2005 Professional Edition
- Microsoft Visual Studio 2005 Standard Edition
- Microsoft Visual Studio 2005 Team System Architect Edition
- Microsoft Visual Studio 2005 Team System Developer Edition
- Microsoft Visual Studio 2005 Team Foundation
SUMMARY
This step-by-step article describes how to switch between forms by using the My.Forms object in a Microsoft Windows Forms application that is built by using Microsoft Visual Basic 2005.
INTRODUCTION
To display a form in a Windows Forms application that is built by using Visual Basic 2005, use either the Show method or the ShowDialog method of the form.
Note Use the My.Forms object to acquire a reference to a form.
When you use the ShowDialog method, the form is displayed as a modal dialog box. Alternatively, you can set the Visible property of the form to True to display a form.
To hide a form, use one of the following methods:
- Use the Hide method.
- Set the Visible property to False.
MORE INFORMATION
To switch between forms by using the My.Forms object in a Windows Forms application that is built by using Visual Basic 2005, follow these steps:
- Start Microsoft Visual Studio 2005.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, select Visual Basic as the project type, select Windows Application as the template, and then click OK.
Note By default, a form that is named Form1 is created. - Add a Button control to the Form1 form.
- On the Project menu, point to Add Windows Form, and then click Add.
Note By default, a form that is named Form2 is created. - Add a Button control to the Form2 form.
- In Solution Explorer, double-click Form1 to open the Form1 form designer window.
- Right-click Button1, click Properties, and then set both the Name property and the Text property to myButton1.
Double-click myButton1 to open the code window, and then add the following code example to the myButton1_click event handler in the Form1 class.
Me.Visible = False My.Forms.Form2.ShowDialog()
- In Solution Explorer, double-click Form2.
- Right-click Button1, click Properties, and then set both the Name property and the Text property to myButton2.
Double-click myButton2 to open the code window, and then add the following code example to the myButton2_click event handler in the Form2 class.
Me.Visible = False My.Forms.Form1.Visible = True
- Build the solution, and then run the application. To do this, follow these steps:
- On the Build menu, click Build Solution.
- On the Debug menu, click Start Without Debugging.
Note The Form1 form appears. - On the Form1 form, click Button1.
Note The Form1 form is hidden, and the Form2 form appears. - On the Form2 form, click myButton.
Note The Form2 form is hidden, and the Form1 form appears.
- Close either form to exit the application.
REFERENCES
For more information about how to create Windows applications, visit the following Microsoft Developer Network (MSDN) Web site:
Keywords: kbhowto kbinfo KB914354