How to Connect Two Forms in Visual Basic

Save

Visual Basic's traditional windows form projects provides two methods for opening new forms. The Show Dialog method opens the second form in dialog 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 dialog with the user. The Show method opens the second form independently, allowing both forms to run in parallel.

Things You'll Need

  • Microsoft Visual Basic Express 2010 or Microsoft Visual Studio 2010

Using the ShowDialog Method

  • Open Visual Basic and create a new Windows form project. Click "File," then "New Project". When the New Project dialog 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.

  • 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.

  • 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
  • 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 dialog form.

Using the Show Method

  • 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."

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

    Close()

  • 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.

References

Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!