VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 4

Showing and Hiding a Form in Visual Basic 6 (VB6)

Loading and unloading only bring the form into memory or remove the form from memory. If the programmer wants to display the form for the user to interact with, another set of commands must be used. The Show and Hide methods affect the form's visibility. Unlike the Load and Unload statements, which you write in code before the form name, Show and Hide follow standard method syntax and precede the object name, separated from it by a period (.).

When a form is to appear on-screen for the user to interact with, the Show statement causes the form to appear. Hide will make the form invisible, but allows it to remain in memory.

If the form is to be directly shown to the user, only the Show method is required. The loading of the Form object will take place automatically.

The following lines of code are all that is required to both load the form and have it displayed:


To understand why the form does not have to be explicitly loaded, first always remember that any programmatic reference to an object will cause it to automatically load. That explains why using the object name followed by the Show method causes the form to load first and then display onscreen. For further discussion of implicit loading, see the previous section in this chapter "Loading and Unloading Forms."

Calling a form's Show method will also cause the form to become the application's active form—that is the form where focus resides in the application. Usually the form itself does not have focus but rather a control on the form. This is because a form object itself cannot get focus unless the form contains no controls that are currently able to receive focus.

If the form is no longer required to be on-screen, it can be removed from display by just using the Hide method. This keeps the form loaded but removes it from display. The following code demonstrates this:


The first line of code removes the form named Form1 from the onscreen display. The second line of code removes the object named frmTest. Both prevent user interaction with the form and help you avoid a very busy screen.

Forms can be hidden instead of being unloaded. If a form is hidden without being unloaded and destroyed, then the values of controls will remain as the user entered them. Other code in the project can then refer to the contents of the controls on that form.

If the form were unloaded, this would re-initialize the controls on the form. Every time the user wanted the settings, they would either have to be loaded from an external source or reset by the user.

The relationship between Show and Hide methods and the Loading and Unloading of forms is also discussed in Chapter 6, "Writing Code that Processes Data Entered on a Form," in the sections "Show/Hide Methods Versus Load/Unload Statements" and "How Significant Form Methods Affect Form Events." It is also discussed in this chapter in the previous section "Loading and Unloading Forms."

Creating Data Input Forms and Dialog Boxes topics

See Also


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved