
|
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.
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.
To show a form, you use its Show
method. frmSettings.Show If you want the form to be modal (the user must close it to return to the program), type: frmSettings.Show vbModal The above is useful for dialog boxes: you must
close them to continue using the program. Unload formname
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. 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.
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.
The following is the code for the module: Public
Name 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. Name = txtName.Text
'set
1st var with textbox data 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: 'The
two global variables are valid also here Last thing is the code for the Back button ... frmData.Show
'show
the other form ... and that for the Confirm button: End 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 programs", projects.
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. Conclusion So, our journey in the world of VB seems to be
over... wrong! It has just begun.
Goodbye for now!
|
|
Previous: Lesson 4 |
Next: The End |