|Forms and message boxes|
|Difficulty: Lessons: 1, 2, 3, 4|
by Lorenzo Dalla Vecchia
Modal versus modeless
You surely know that there are some forms in the Windows environment that you must make a choice and close to continue using the program they belong. A common example is the Open (or Save as) dialog box: you must first choose a file (make a choice) and then click OK or Cancel (close the form). That is, a modal form. In the contrary, "common" forms (such as the main form of your project) you can switch between without closing, are of the modeless type.
A modal form can be very useful in your project, because of two reasons:
|Let's see how to show
a form in modal mode; it is simple: just add the vbModal
parameter to the Show method.
If you want a form to be modeless, just type:
Or just omit the parameter (as we used to do previously).
It's time to verify the features of modal and modeless forms with a sample project. Start a new project and put a command button on Form1, naming it cmdOpenForm and setting its Caption to "Open Form 2".
Then, add a new form (Form2) to the project. Put three controls on it: a label (lblEnterNumber), a textbox (txtEnterNumber) and a command button (cmdClose). We want the latter button to hide (not unload) Form2, so this is the code for the Click event:
Me.Hide 'or Form2.Hide
Now, back to Form1. We want the cmdOpenForm button to open Form2 and show the number entered in txtEnterNumber in a message box. Here's the code for the button's Click event:
MsgBox "You entered the number " & Form2.txtEnterNumber.Text
Ok? Now run the project and click on the button to open
Form2: a message box will immediately pop up with the message "You entered the
number" but without the number, and that's obvious, because you have not typed it
yet. Why did the message box pop up because you closed Form2? It happened because Form2 is
shown in modeless mode, and that's mean the code after Form2.Show is executed immediately after the
form has been loaded. Another thing you can test is that you can close Form1 without
closing Form2 first... and that's not fine!
To prevent those troubles, replace Form2.Show with:
Run the program again: it should now work perfectly. Also note that, if you click on Form1 while Form2 is shown, you will hear a beep: it happens because, when a modal form is shown, all other forms (within the same program) are disabled except for the modal form itself.
In the Windows environment each form can be in one of three states:
The user can "switch" between the three states by using the buttons in upper-right corner of the form:
|You can also switch
between states from code using the WindowState property. The available
For example, the following minimizes Form3:
Form3.WindowState = 1
As an example, let's see how to create a full screen
application (i.e. a game)
that can be minimized through a command button.
At design time, set the WindowState property of the form to 2 (Maximized) and try running it: you will still see the title bar on the upper side of the screen. So, we need to hide it: set the BorderStyle property to 0 and try again. The form will cover the entire screen (this is how screen savers are made); to close the program, press the key combination CTRL+PAUSE and then click the Stop () button.
|Sometimes, because of errors in your program, you will not be able to stop it in the usual ways. The solution is using the key combination CTRL+PAUSE to halt your program and switch to Visual Basic. You can then use the Stop button to go back to design.|
Now add two command buttons to the form: cmdExit and cmdMin.
We will use the first to close the program and the second to minimized the form.
The code for cmdExit is very simple:
Unload Me 'unload the form
End 'terminate the program
The End statement is not strictly required (unloading the only form automatically
causes program termination), but it is better to type it each time you want to stop your
program and not just unload a form.
Last thing, the code for the cmdMin button. As you were probably thinking, it is:
Me.WindowState = 1
Run the program and watch how it works.
The code behind a form
Being special type of modules, forms usually contain a lot of code: although the most of it is often assigned to controls, a form can have code both attached to its own events and located in the Declarations section.
As almost all controls, a form has a set of events with associated event procedures. To access them, do the following:
We will now see some events that can be useful when programming a form:
There are many other events, such as Click, DblClick, DragDrop, DragOver, KeyDown, KeyPress, KeyUp, MouseDown, MouseMove, MouseUp. Those events are not relative to forms only, so we will study them in detail another time in a more general context.
Another place you can put code in a form is the General Declarations section. To view it:
The General Declarations section can contain code not associated with any control/event.
Ok, that's all about forms. You should now have complete
control over this important aspect of Visual Basic programming.
The last part of this tutorial will cover the topics of message boxes and input boxes.
|Visual Basic Programming Zone is a website by
Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it