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
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:
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."
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.
How to Code a Login/Registration Form in VB
Coding a login and a registration form in Microsoft Visual Basic.NET (VB.NET) can make your applications more secure. A program that uses...
How to Connect VB to Excel
It's easy to learn how to connect and use Visual Basic (VB) inside of the Microsoft Excel application. VB is an excellent...
How to Create Forms With Visual Basic
Since it was first released by Microsoft in 1987, Visual Basic (VB) has developed into one of the United States' leading programming...
How to Make a Search Form for a Visual Basic Project
Most computer programs connected to a database file allow end users to view and manipulate data without having to interact directly with...