A command button is a control that allows the user to
make a decision based on what the form is displaying. This could be a
simple acknowledgement such as a form displaying a quick message to the
user. It could also require the user to make other selections or perform
some settings on a dialog box before continuing to other issues.
As a programmer, the computer and the user count on
you to decide on the role of a button. And as stated already, a button can
assume any role you assign it to.
|
Practical Learning: Inserting A Command Button
|
|
- On the Toolbox, double-click the CommandButton
- On the Properties window, make the following changes:
(Name) |
cmdEmployees |
Left |
120 |
Caption |
Employees |
Top |
3120 |
Height |
375 |
Width |
1215 |
- To test the form, press F5 (of course, the button doesn't do much
because we didn't ask it to do anything).
- There are many ways you can access the area where you will be
writing code. Usually, the code you write will be associated with one
of the controls on your form. Although many behaviors are associated
with each control, every control, including the form itself, has a
default behavior. For example, the default behavior of a button is the
click action.
To access the code associated with the first button, double-click
Employees.
This opens the Code Editor window.
- Notice that the mouse cursor is inside of the click event code
associated with the cmdEmployees button.
Change the code of the event as follows:
Private Sub cmdEmployees_Click()
frmEmployees.Show
End Sub
- To test the effect of the new code, on the Standard toolbar, click
the Start button
- When the main form is loaded, click the Employees button. Notice
that the second displays.
- To close the Employees Records form, click its Close button
- Notice that the caption did not change on the main form, although it
would change if you had double-clicked inside of the form.
- To close the main form, click its Close button .
- On the Toolbox, double-click CommandButton.
- On the Properties window, make the following changes:
(Name) |
cmdExit |
Left |
3480 |
Caption |
Exit |
Top |
3120 |
Height |
375 |
Width |
1215 |
-
Earlier you saw that when a form is loaded, if you click the End
button on the Standard toolbar, the form is closed. Now, we will write code
that will tell Microsoft Visual Basic that when we click the Exit button, we want it to behave as if we had clicked the End button
on the Standard toolbar.
On the main form, double-click the Exit button.
-
Press Tab and type End so your code appears as follows:
Private Sub cmdExit_Click()
End
End Sub
- To test the functionality of the form as this time, on the Standard
toolbar, click the Start button
- Click the Employees button
- To close the Employees Records form, click its close button .
- To close the main form, click Exit.
- To save the project, on the main menu, click File -> Save Project
|
A label is a control used to display text on a form.
This control is for two main purposes: to provide quick information or to
help the user identify another control on the form.
The properties of a label are completely under your
control; the user cannot manipulate them. Although you can change these
properties at will, make sure you set their appropriate values in the
Properties window. Always avoid distracting the user.
|
Practical Learning: Creating A Label
|
|
- To create a new project, on the main menu, click Filed -> New
Project
- On the opening dialog box, make sure Standard EXE is selected and
click Open.
- On the Toolbox, click Label.
- On the form, draw a rectangular box from the top left section to the
right section.
- On the Properties window, click (Name) and type lblFormTitle
- Click the Alignment field, click the arrow of its combo box and select
2 - Center
- Click Caption and type Rockville Auto-Mart
- Click the Font field. Click its Build button
to call the Font dialog box.
- On the Font dialog box, set the font to Times New
Roman, Style = Bold,
and Size = 22.
- Click Height and type 495
- Click Left and type 120
- Click Top and type 120
- Click Width and type 4500
- To test the form, press F5
- To close the running form, click its close button .
- On the Toolbox, double-click Label.
- Using the label's handles, move it under the main title and on the
left.
- By double-clicking and moving the controls, create the following four
labels:
(Name) |
lblFirstName |
lblLastName |
lblFullName |
lblMessage |
Caption |
First Name |
LastName |
Full Name |
Empty |
Height |
255 |
255 |
255 |
255 |
Left |
120 |
120 |
120 |
120 |
Top |
705 |
1125 |
1560 |
2160 |
Width |
855 |
855 |
855 |
3975 |
- To test the form, press F5.
- After viewing the form, click Close button .
|
|
|