How to: Communicate Between Forms in an Application

This example shows how to use the My.Forms object to access one form from another form.

The My.Forms object provides access to an instance of each form in a Windows application that is declared in the application's project. This allows code in one form to communicate with another form.

To create code in one form that can communicate with another form

  1. Create a new Windows Application.

    The default name of the main form is Form1.

    For more information, see How to: Create a Windows Application Project.

  2. Add another form named Form2.

    For more information, see How to: Add Windows Forms to a Project.

  3. Add a button named Button1 to Form1 in Application Designer.

  4. Double-click Button1.

  5. In the event handler for the Click event of Button1, add the following code:

    My.Forms.Form2.Text = Now.ToString
    My.Forms.Form2.Show()
    
  6. Run the application.

  7. Click Button1.

    Form2 opens, and its title shows the current time.

  8. Wait a few seconds and click Button1 again.

    Form2 remains open and its title updates to the current time.

See Also

Tasks

How to: Access All Open Forms of an Application

Concepts

Accessing Application Forms

Reference

My.Forms Object