|Learn Visual Basic|
|Difficulty: Lessons: 1, 2, 3, 4, 5|
by Lorenzo Dalla Vecchia
Up to now, we always worked on the same, plain, blank form that VB creates for us, but have you ever seen a program made up of only one form? Maybe, but I think you agree with me if I say that most programs contain multiple forms.
The first thing you need to learn is that a form (that is just a control) can be of different types: resizable forms (those you already know), fixed-size forms, border-less form, dialog boxes, toolboxes. Take a look at the following pictures: you should already have seen them around Windows.
Can't be resized.
|BorderStyle = 0|
|Fixed-size.||Caption with icon.
Can't be resized.
|BorderStyle = 1|
|Resizable||Caption with icon.
Can be resized.
|BorderStyle = 2|
|Dialog Box||Caption w/o icon.
Can't be resized.
|BorderStyle = 3|
|Fixed-size Toolbox||Small caption w/o icon.
Can't be resized.
|BorderStyle = 4|
|Resizable Toolbox||Small caption w/o icon.
Can be resized.
|BorderStyle = 5|
The BorderStyle thing on the very right of the table is the value you must set the BorderStyle property of the form to change its appearance. The BorderStyle property can only be changed at runtime, through the Properties window.
So, now that you can create many type of forms, you can have multiple forms in your program. To differentiate them, as for all controls, you use names. When you create new forms, they are given the names Form1, Form2, Form3, ... It is better to rename them something understandable.
|The abbreviation for the form control is frm.|
To add a new form to your program, select Add Form from the Project menu, or click the corresponding button on the toolbar.
To show a form, you use its Show method.
If you want the form to be modal (the user must close it to return to the program), type:
The above is useful for dialog boxes: you must close them
to continue using the program.
To close a form, you type:
To switch between forms at design time, you use the Project
Found at the right of your screen, it shows you the list of forms in your program in hierarchical format.
To select a form, click its name, and then click on one of the two buttons at the top-left: the first shows the Code View for the form, the other shows the Form View (where you can put controls on the form).
And with the above, the argument of forms is exhausted.
Modules, code containers
In the Variables chapter I told you that the
General Declarations section of a form is a "neutral area" that affects the
whole form, right? Any declaration (Dim statement), sub or function you
put there is accessible from anywhere within the form.
But now that we've seen how to work with multiple forms, we need an even more "neutral" area that affects the whole program. This thing is called a module.
A (standard) module is like a form
without graphical interface: it is just a code container, consisting only of
a General Declarations section, accessible from anywhere within your program.
Adding a module is similar to adding a form: select Add Module from the Project menu.
This is what you will see.
As you see, you can't almost distinguish it from a form code window. To know you are inside a module, look at the Project Explorer on the right: the element Module1 is selected. Also note that, when a module is selected, the Form View button (the 2nd from left) is disabled, because, as I said before, modules contain only code, not controls.
|The module itself is a kind of control: when you're inside a module, look a the Properties window and you will see its only property, Name, that you can set as you do for forms. The abbreviation for the module object is mod.|
So, let's make a little program with two forms and a
module. Name the first form frmData and put two textboxes (txtName and txtLastName) with
corresponding labels and a command button (cmdOK) on it.
Name the other form frmConfirm and put two labels (lblName and lblLastName) and two buttons (cmdConfirm and cmdBack) on it. We want the user to enter information in the first form, click OK, verify information on the second form and confirm (exit the program) or not (go back to first form).
Because we need to retain data between two forms, create a module (modVariables) to declare two String variables.
The following is the code for the module:
Public Name As String
Public LastName As String
The above two variables have a global scope
(or public), as opposed to form-level scope (or private)
and procedure-level scope (see the chapter about Variables).
Note that this time we declared the variables with a new statement, Public:
it works exactly as Dim, but expressly gives them public scope. For these reasons, Public
can only be used in modules; in all other cases you will continue using Dim.
The two variables must be filled when the user clicks on the OK button on the first form. This is the code:
Name = txtName.Text
'set 1st var
with textbox data
LastName = txtLastName.Text 'set 2nd var with textbox data
frmConfirm.Show 'load the other form
Unload frmData 'unload current form
The two variables are needed on the other form to update labels. Type the following into the Load event procedure of frmConfirm, executed each time the form is loaded:
global variables are valid also here
lblName.Caption = Name 'set 1st label with var data
lblLastName.Caption = LastName 'set 2nd label with var data
Last thing is the code for the Back button ...
'show the other form
Unload frmConfirm 'unload current form
... and that for the Confirm button:
As a conclusion, you should know that the module is not a particular type of form, but its the form a particular type of module that has graphical interface. You will often hear programmers refer to forms and modules as modules and refer to private variables such as module-level variables.
Dealing with a "project"
Now that you have all the basic knowledge about VB programming, you can start designing your very own applications.
In the First Look at VB chapter, you already
learned how to create a new application. Visual Basic calls this kind of "unfinished
A Project is the collection of forms, modules, pictures, ... in your application.
To save the current project (your entire work!), simply select Save from the File menu in VB. The project won't be saved in a single file, but "splitted" in many parts:
So, if you move your project files, remember to copy all of them. To open your saved project, just open the project (PRJ) file and VB will load all the forms and modules associated with it.
"All OK now?" No, because your projects need an
installed copy of Visual Basic to work - not a good thing, uh? If you want to
distribute your applications, you must first compile them into a Windows
EXE file. To do this, select the Make... command from the File
menu, choose a location and click OK.
Also remember that, if you want to distribute your application, you need to create a Setup program on a set of floppy disks; don't worry, this is done automatically by Visual Basic with a program known as Package and Deployment Wizard (if you have VB6) or Application Setup Wizard (if you have VB5). You can find it in the Start Menu in the VB group.
So, our journey in the world of VB seems to be over...
wrong! It has just begun.
Continue practice with Visual Basic and start making your own programs: you will make mistakes, but even "expert" programmers do (direct experience), and it is the best way to learn.
There are many resources that can help you:
Goodbye for now!
|Visual Basic Programming Zone is a website by
Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it