Home > Articles

0672320800

  • Print
  • + Share This

Showing and Hiding Forms

Knowing how to create forms does nothing for you if you don't have a way to show and hide them, because Visual Basic can display a single form automatically only when a program starts. To do this you have to write code.

Showing Forms

In Visual Basic.NET, everything is an object, and objects are based on classes. Because the definition of a form is a class, you have to create a new Form object using the class as a template.

The process of creating an object from a class (or template) is called instantiation. The syntax you'll use most often to instantiate a form is the following:

Dim {objectvariable} As New {formclassname()}

The three parts of this declaration are

  • The Dim statement A reserved word that tells the compiler that you're creating a variable.

  • The name of the variable This will be the name for the form that you will use in code.

  • The keyword New Indicates that you want to instantiate a new object for the variable.

Lastly, you specify the name of the class to use to derive the object—your form class. If you have a form class named fclsLoginDialog, for example, you could create a new Form object using the following code:

Dim frmLoginDialog As New fclsLoginDialog()

Thereafter, for as long as the object variable remains in scope, you can manipulate the Form object using the variable. For instance, to display the form, you call the Show method of the form or set the Visible property of the form to True using code such as this:

frmLoginDialog.Show

or

frmLoginDialog.Visible = True

The easiest way to get the hang of this is to actually do it. To begin, choose Add Windows Form from the Project menu to display the Add New Item dialog box. Change the name of the form to fclsMyNewForm.vb (as shown in Figure 5.5), and click Open to create the new form.

Figure 5.5. When you change the default name of a form, don't forget to leave the .vb extension.

Your project now has two forms, as you can see by viewing the Solution Explorer window. The new form is displayed in the form designer, but right now you need to work with the main form. At the top of the main design area is a set of tabs. Currently, the tab fclsMyNewForm.vb [Design] is selected. Click the tab titled frmExample.vb [Design] to show the designer for the first form. Add a new button to your form by double-clicking the Button item on the toolbox. Set the button's properties as follows:

Property

Value

Name
btnShowForm
Location
112,112
Text
Show Form

Double-click the button to access its Click event, and enter the following code:

Dim frmTest As New fclsMyNewForm()
frmtest.Show()

The first statement creates a new object variable and instantiates an instance of the fclsMyNewForm form. The second statement uses the object variable, now holding a reference to a Form object, to display the form. Press F5 to run the project and click the button. (If the button doesn't appear on the form, you may have accidentally added it to the wrong form). When you click the button, a new instance of the second form is created and displayed. Move this form and click the button again. Each time you click the button, a new form is created. Stop the project now and click Save All on the toolbar.

Unloading Forms

After a form has served its purpose, you'll want it to go away. However, "go away" can mean one of two things. First, you can make a form disappear without closing it or freeing its resources (this is called hiding). To do so, set its Visible property to False. This hides the visual part of the form, but the form still resides in memory and can still be manipulated by code. In addition, all the variables and controls of the form retain their values when a form is hidden, so that if the form is displayed again, the form looks the same as it did when its Visible property was set to False.

Second, you can completely close a form and release the resources it consumes. You should close a form when it's no longer needed, so Windows can reclaim all resources used by the form. To do so, you invoke the Close method of the form like this:

Me.Close()

Because Me represents the current Form object, you can manipulate properties and call methods of the current form using Me. (Me.Visible = False, and so forth).

The Close method tells Visual Basic to not simply hide the form, but to destroy it completely. If variables in other forms are holding a reference to the form you close, their references will be set to Nothing and will no longer point to a valid Form object.

Select the fclsMyNewForm.vb [Design] tab to display the form designer for the second form, add a new button to the form, and set the button's properties as follows:

Property

Value

Name
btnCloseMe
Location
112,112
Text
Close Me

Double-click the button to access its Click event and then enter the following statement:

Me.Close()

Next, run the project by pressing F5. Click the Show Form button to display the second form, and then click the second form's button. The form will disappear. Again, the form isn't just hidden; the form instance is unloaded from memory and no longer exists. You can create a new one by single-clicking the Show Form button on the first form. When you're finished, stop the running project and save your work.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.