Forms

Hiding a form

When a user has finished with a dialog, they will want to close it by clicking a OK button of some sorts. To hide a dialog, you can use the Hide method. The following code hides Dialog1.

Dialog1.Hide

Using the Hide method does what is says. It hides the dialog. The dialog is not actually removed from the memory. When you use the Show method again, any controls will have kept their values. This is useful if the dialog will be shown and hidden a lot. However, forms such as the Splash form that will only be shown once should be removed from the memory. To do this, use the Unload method. If you do not do this, you will end up with lots of forms in the memory, not being used. If you have many forms with lots of controls saved in the memory, you may find you application eats up all the avaiable memory, and the whole system crashed. If you need to keep some values, save them to a variable, which can be accessed again when you need to load the form again. The following code removes Dialog1 from the memory.

Unload Dialog1

A very common mistake is to use the End statement. This is not good practice, as it does not always free up any of the memory that was allocated for your application.

When you Unload a form, its QueryUnload and Unload events occur. In both events, you can cancel the form unloading by setting Cancel = -1. In the QueryUnload event, you can also find out how the form is being unloaded by checking the UnloadMode value. This can be one of the following:

Constant Description
vbAppTaskManager (3) Windows Task Manager is closing the application.
vbAppWindows (2) Current Windows session ending.
vbFormCode (1) Unload method invoked from code.
vbFormControlMenu (0) User has chosen Close command from Control menu box on form, or clicked the X button.
vbFormMDIForm (4) MDI child form is closing because the MDI form is closing.
vbFormOwner (5) The owner of the form is closing.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The question of whether computers can think is just like the question of whether submarines can swim.” - Edsger W. Dijkstra