|Forms and message boxes|
|Difficulty: Lessons: 1, 2, 3, 4|
by Lorenzo Dalla Vecchia
With a fairly good knowledge of Visual Basic, the next step is knowing how to work with the base of an application's user interface: forms.
Forms are double-sided modules
You may know that a Visual Basic project (the collection of files that makes up the final application) is made up of modules. Many types of modules exist: standard modules, class modules, ... and form modules. While the first two act as simple "code containers", a form module contains more than just code: it has a graphical interface.
To discover those two sides of a form, open VB, create a new project and draw some controls on the form.
As you can see, I drew four controls (a textbox, a timer, a label and a command button) on the form named Form1. Now, double click on the form and you will see something like this:
This is the other side of the form: the Code View. It is the place to type the code that will bring the form and its controls to life.
you can see, there are two dropdown lists on the top. Try opening the one on the left:
there we find Text1, Timer1, Command1 and Label1. Each
item leads to the event procedures for the corresponding control. For
example, click on Command1, and from the list on the right you will be able to
choose one of Command1's events and edit the code associated to it: VB automatically
creates empty event procedures for all the events and they are delimited by the
instructions Private Sub and End Sub.
The list on the left also contains two other items: Form and (general): the first leads to the code associated with the form itself (Form1 in our case) and with its events. The(general) item is used to access the General Declarations Section of the form: it contains all the code that is not associated with any event, such as variable declarations, subs and functions. To learn more, read the Learn Visual Basic tutorial, lesson 4.
Forms in the VB environment
In the every-day language, the word form is used to refer to paper modules you need to fill in with data; the idea of the form is as simple as that: something the user can fill in with data for the application. Forms are more commonly referred as windows or boxes.
Working in the Microsoft Windows environment, you notice the following two things:
So I will now explain how to add/remove a form and how to control their
To add a new form to your project, do the following:
The new form will show up, ready to be filled with controls; you will see a new item added in the Project Explorer on the right.
To access the Code View for the form, double-click on the form or click on the first button in the Project Explorer.
To access the Form View (graphical interface), click on the second button in the Project Explorer.
To delete a form from the project (but not from the hard disk), right click its name in the Project Explorer and choose Remove.
NOTE: remember that each form in your project is saved into a .FRM file on the hard disk. The first time you save your project after a new form has been added, you'll have to choose the location for the .FRM file. I suggest saving it in the same folder of the main project file.
Manage forms from code
First of all, you need to know that a form can be:
When your application starts, only the main form is shown, all the others are unloaded.
|To load a form into memory,
use the Load statement as follows:
The above will not show the form. To show the form, fire its Show method:
If you want to close a form, you must decide either to hide it or unload it.
To hide a form, fire its Hide method:
The above will just make the form disappear from the
screen, but it will remain in memory: that means that all the data entered in, for
example, textboxes, will be preserved.
However, in most of the cases you will use the Unload statement to free up memory and speed up your program:
Let's try an example project. On the default Form1 put
two command buttons, name them cmdOpenForm and cmdShowText and give them the captions
"Open Form2" and "Show entered text" respectively.
Now add a new form (Form2) to the project; put a textbox and two command buttons on it, and name them txtText, cmdHideForm and cmdCloseForm. The captions for the buttons are "Hide this form" and "Close this form".
Here's Form1. We want the first button to open the other form (Form2). It is easy, simply enter the following code in the Click event procedure:
As I said before, this loads and shows the form. If Form2
is already loaded but hidden, the above code will just show it.
The other button should show the code typed in the txtText textbox on Form2. Usually you would type something like MsgBox txtText.Text, but in our case txtText in located on another form, and we need to use the following syntax:
Now, our code ...
The above is a simple example of object hierarchy: Text is one of txtText's properties and "belongs" to it; txtText in one of Form2's controls and is contained into it. From left to right, objects assume decreasing hierarchy levels. Note that dots (.) are used to divide them.
On the right you can see Form2, let's write code for its controls.
The first button will hide the form (but not unload it), the other will unload it completely. The two pieces of code are, respectively ...
... and ...
And that's all the code the form needs.
|What is the Me keyword I used in place of Form2? The Me keyword can be used to refer to the container form (the form containing the code typed). It's a matter of style: it would have been the same good if you typed Form2.Hide and Unload Form2.|
Let's do some tries. Run the project and try the following:
In the next chapter, I will explain how to position forms on the screen.
|Visual Basic Programming Zone is a website by
Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it