How to switch between forms in a Windows Forms application that is built by using Visual Basic 2005

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

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:
  1. Start Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. Add a Button control to the Form1 form.
  5. On the Project menu, point to Add Windows Form, and then click Add.

    Note By default, a form that is named Form2 is created.
  6. Add a Button control to the Form2 form.
  7. In Solution Explorer, double-click Form1 to open the Form1 form designer window.
  8. Right-click Button1, click Properties, and then set both the Name property and the Text property to myButton1.
  9. 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() 
  10. In Solution Explorer, double-click Form2.
  11. Right-click Button1, click Properties, and then set both the Name property and the Text property to myButton2.
  12. 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
  13. Build the solution, and then run the application. To do this, follow these steps:
    1. On the Build menu, click Build Solution.
    2. On the Debug menu, click Start Without Debugging.

      Note The Form1 form appears.
    3. On the Form1 form, click Button1.

      Note The Form1 form is hidden, and the Form2 form appears.
    4. On the Form2 form, click myButton.

      Note The Form2 form is hidden, and the Form1 form appears.
  14. 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:
http://msdn2.microsoft.com/en-us/library/Aa984088(VS.71).aspx

Properties

Article ID: 914354 - Last Review: November 22, 2007 - Revision: 1.5
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
Keywords: 
kbhowto kbinfo KB914354

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