Wednesday, September 2, 2009

Know Form In Visual Basic 6.0


The Appearance of Forms


The main characteristic of a Form is the title bar on which the Form's caption is displayed. On the left end of the title bar is the Control Menu icon. Clicking this icon opens the Control Menu. Maximize, Minimize and Close buttons can be found on the right side of the Form. Clicking on these buttons performs the associated function.
The following figure illustrates the appearance of a Form

The control menu contains the following commands :
  • Restore : Restores a maximized Form to the size it was before it was maximized; available only if the Form has been maximized.
  • Move : Lets the user moves the Form around with the mouse
  • Size : Lets the user resizes the control with the mouse
  • Minimize: Minimizes the Form
  • Maximize : Maximizes the Form
  • Close : Closes the Form

Setting the Start-Up Form

A typical application has more than a single Form. When an application runs the main Form is loaded. By setting the Project properties you can control which Form is to be displayed in the Start-Up of the application. Following figure illustrates the Project property window.

By default, Visual Basic suggests the name of the first Form created when the project started.

Loading and Unloading Forms

In order to load and unload the forms, Load and Unload statements are used. The Load statement has the following syntax :
Load FormName
And the Unload statement has the following syntax :
Unload FormName
The FormName variable is the name of the Form to be loaded or unloaded. Unlike the Show method which cares of both loading and displaying the Form, the load statement doesn't show the Form. You have to call the Form's Show method to display it on the desktop.

Showing and Hiding Forms

Show method is used to Show a Form. If the Form is loaded but invisible, the Show method is used to bring the Form on Top every other window. If the Form is not loaded, the Show method loads it and then displays it.
Syntax of the Show method of the Form
FormName.Show mode
The FormName variable is the Form's name, and the optional argument mode determines whether the Form will be Modal or not. It can have one of the following syntax :

*
0-Modeless (default)
* 1-Modal
Modeless Forms are the normal Forms. Modeless Forms interact with the user and the user allowed to switch to any other Form of the application. If you do not specify the optional mode argument, by default the mode is set to modeless.
The Modal Forms takes the total control of the application where user cannot switch to any other Forms in the application unless the Form is closed. A modal Form, thus, must have a Close button or some means to close the Form in order to return to the Form where the Modal Form was loaded.

Hiding Forms

The Hide method is used to hide a Form. The following is the syntax of the Hide Method.
FormName.Hide
To hide a Form from within its own code, the following code can be used.
Me.Hide
You must understand that the Forms that are hidden are not unloaded ; they remains in the memory and can be displayed instantly with the Show Method. When a Form is hidden, you can still access its properties and code. For instance, you can change the settings of its Control Properties or call any Public functions in the Form.
The following is an example illustrates the Show method and Mode statement
* Open a new Project and save the Project
Design the application as shown below
Object
Property
Setting
Form Caption
Name
Form1
frm1
Form Caption
Name
Form2
frm2
Form Caption
Name
Form3
frm3
Label Caption
Name
Click on a button to display a Form
Label1

The following code is typed in the Click event of the command buttons

Run the application. Clicking on the buttons will display the Forms respectively. But you can see that in the cmd2_Click( ) event additionally VbModal argument has been added. You can see the difference after you display the forms by clicking on the command buttons. You can notice that you cannot switch to any other Forms in the application unless you close the Form3.

Finding out the difference between Unload and Hide method

To know what the difference is between Unload and Hide methods we will do an example. Open a new project and save the project. Draw two buttons on the form and name those as shown above.

In the click event of the Hide button Following code is entered.
Me.Hide
In the click event of the Unload button following code is entered.
Unload Me
Save the project and run the application. Once you click on Hide button you can note that the Form is invisible but the application is still running. But when you click on Unload button you can see that the application is terminated. (Download the source code)

0 comments:

Your Ad Here