How to connect two forms in visual basic

Written by rick leander
  • Share
  • Tweet
  • Share
  • Email

Visual Basic's traditional windows form projects provides two methods for opening new forms. The Show Dialog method opens the second form in dialogue mode, suspending the first form until the user responds to the second form. This type of form works well for logging in, accepting settings or conducting a dialogue with the user. The Show method opens the second form independently, allowing both forms to run in parallel.

Skill level:
Moderately Challenging

Other People Are Reading

Things you need

  • Microsoft Visual Basic Express 2010 or Microsoft Visual Studio 2010

Show MoreHide

Instructions

    Using the ShowDialog Method

  1. 1

    Open Visual Basic and create a new Windows form project. Click "File," then "New Project". When the New Project dialogue appears, choose "Windows" from the tree view, choose "Windows Forms Application" from the project types, name the project "FormSamples," then click OK. A new form will appear.

  2. 2

    Drag a label and three buttons onto the new form. Right-click on the label and choose "Properties." Name the label "lblTitle" and enter "Welcome" in the Text property. Click "ESCAPE" to return to the form designer. Repeat the process for each of the buttons, entering "Log In," "View" and "Close" for each button's Text property.

  3. 3

    Create a second form. Click "Project" then "Create New Windows Form." Name the form "LoginForm." Drag a label, textbox and two buttons onto the form. Change the Text property of the label to "Name:," name the text box "txtName" and label the buttons "Log In" and "Cancel."

    Double-click the Login button to open the code window and add the following code:

        DialogResult = DialogResult.OK
    

    Click View then Design to return to the form design view. Double-click the Cancel button and add the following code:

        DialogResult = DialogResult.Cancel
    
  4. 4

    Return to the first form by clicking "Form1" in the Solution Explorer window. Double-click on the Log In button and add the following code:

        Dim frm As New LoginForm
    
        If frm.ShowDialog = DialogResult.OK Then
    
            lblTitle.Text = "Welcome " & frm.txtName.Text
    
        End If
    
        frm.Close()
    

    This code creates a new instance of the login form then uses its ShowDialog method to open the form. If the login button is clicked, Showdialog returns DialogResult.OK and the code copies the name from the text box into the welcome message. If "Cancel" gets clicked, this code is ignored. The Close method closes the dialogue form.

    Using the Show Method

  1. 1

    Create another form called "ViewForm." Drag a label and a button from the toolbox onto the form. Change the Name property of the label to "lblContent" and change the Text property of the button to "Close."

  2. 2

    Double-click the "Close" button and add the following code:

    Close()

  3. 3

    Return to the form designer for Form1 then double-click the "View" button. Add the following code:

        Dim frm As New ViewForm
    
        frm.lblContent.Text = lblTitle.Text & ".  Place content here."
    
        frm.Show()
    

    This code creates a new instance of the ViewForm. It then sets the text property of the label and calls the form's show method. Note that there is no Close method and the Show method does not look for a return value.

Tips and warnings

  • Using the techniques above, see If you can add code to handle the event for the Close button on Form1.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.