Basic Form Properties and Modality in VB.NET

A form is the most important object in VB.NET. It is where your program’s users will input data. It is also where your program’s results are shown. In other words, the form is the user interface. This tutorial covers basic form properties and the modality of the form in VB.NET. It is written for VB.NET beginners.

Creating your First VB.NET Form

1. Launch Microsoft Visual Basic 2008 Express Edition. If you do not have this program, then you cannot create VB.NET forms. You can read an introductory tutorial on how to install Visual Basic on your computer: https://www.aspfree.com/c/a/VB.NET/Visual-Basic-for-Beginners/

2. Go to File –> New Project.

3. Since you will be creating a form, select “Windows Forms Application.”

4. Select a name for your form project, e.g. “MyFirstForm.”

5. Hit OK to get started.

6. You will then see an empty form — just like an empty canvas when you paint. It looks like this:

Accessing the Properties of the Form

The properties of the form will be used to configure the form settings. The form includes two accessible, important kinds of properties. These are Development Properties and Physical Properties.

The form’s development properties are used to configure its look and feel, as you can see in the design. These can include the text color, background color, background images, etc. The form’s physical properties are used to configure how the form will be saved in your hard drive, as well as other external file properties.

To access the development properties:

1. Go to View –> Properties Window.

2. On the lower right, you should see “Properties” “Form1” “System.Windows.Forms.Form.”

There are many development properties that you can use to customize your form.

To access the physical properties of the form:

1. Go to View –> Solution Explorer.

2. On your lower right, you should see the “Properties” “Form1.vb File Properties.”

The properties include “Build action,” “Copy to Output directory,” and so forth.

Setting the Form Name, Title Text and Saving the Form Project

The first set of skills that you need to know is how to set the form name, title text and the background color.

Changing the name of the Form

There are actually two names for the form. The first is found in the development properties, and the other is in its physical file properties. The most important of these for VB.NET programming is the name set using development properties.

Bear in mind that if you set a different name for the form in the development properties, the physical name of the form will not be changed. Follow these guidelines for setting the form name in the development properties:

  • Use a short name (one word only).
  • Make it relevant to the form’s application.
  • Do not include any characters (such as dots, asterisks, etc). For simplicity and ease of understanding in the coding process, use only alphabet letters when naming your form.

To change the name of your form:

1. Go to development properties (View –> Properties window).

2. Find the “(name).” This is the name of the form. By default, Microsoft Visual Basic uses the name “Form1.”

3. Change the name from “Form1” to “ExampleForm1.”

4. To check that you have set the form name to your intended name, double click the form in design. The VB.NET program should now use the name of the form in the public class. For example:

Public Class ExampleForm1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
    End Sub
End Class

While the name of the form in development properties is now “ExampleForm1,” the physical name of the form as saved in your hard drive is still Form1.vb (as you can see in Solution Explorer).

Changing the Form Title Text

Some beginners think that the form title is also the form name. This is not the case. The form name is used during the data binds of the VB.NET source code; this is not visible to your user. The form title is visible to your user as text.

To change the title text of the form:

1. Go to Form development properties.

2. Scroll down until you see the “Text” property.

3. Change it from “Form1” to “This is the first example form.” It is recommended that you assign a descriptive title to your form. Make it meaningful, but not too long.

Saving Your Form Project

Finally, you need to save your VB.NET project. Go to File –> Save all. Make sure that the name is set to “MyFirstForm.” Select the folder in your hard drive where you would like to save your project. Make the name the same as the solution name, and make sure “Create a directory for solution” is checked. Now, click "Save." 

Your form should now look like the screen shot below:

{mospagebreak title=Creating Another Form for Your Application Project}

With most applications, you will need to create more than one form. To create another form:

1. Click the form in Design.

2. Go to Project –> Add Windows Form.

3. Under Visual Studio Installed Templates, select “Windows form.”

4. Set the form name to Form2.vb.

5. Click Add.

6. Assign the form the name “ExampleForm2” and the text title of “This is my second form.”

7. Go to File –> Save All.

There should now be two forms in your Visual Basic application project, which will be visible in the Solution Explorer:

Modality of the Form

There are two kinds of forms, depending on how these forms will be used in your application. They are modal forms and non-modal forms.

When a user launches or uses a modal form, the user can only deal with that form. Other forms are disabled or cannot be clicked and edited. A good example of this is the Print Dialog in MS Word. Use the modal form if you would like to force the user to enter required data into that form.

The non-modal form is the opposite. When the non-modal form is used, the user can still work with other forms in the application. A good example of a non-modal form is the “Basic Search” or “Help” dialog in MS Word.

For example, say you would like to launch “ExampleForm2” as a modal form. To make this form modal, you use the form name, and then call the ShowDialog() method:

ExampleForm2.ShowDialog()

To implement this, double click “ExampleForm1” in the design of Form1.vb. And then in the VB.NET code, this is how it’s set:

Public Class ExampleForm1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ExampleForm2.ShowDialog()
    End Sub
End Class

Save all changes, and then run form1.vb by pressing F5. You will notice that you cannot see the first form, only the second form, with the text “This is my second form.” This is because the second form is a modal form. If you close the second form, you can then see the first form.

To make the form non-modal, simply change the method from ShowDialog() to Show(), such as:

ExampleForm2.Show()

Now try pressing F5 again to run the program. This time, you will see the two forms. See the screen shot below:

Aside from the above skills, feel free to experiment with other changes to forms available in development properties, such as setting the background color, text color, etc. They are very easy to learn and somewhat self-explanatory.

[gp-comments width="770" linklove="off" ]