Adding controls to the form
Before you can add controls to a form, you need a form. Right-click any-
where in the Project Explorer window and then choose Insert
➪
UserForm
from the context menu that appears. After you add the form, make sure that
you give it an easily remembered
name by changing the
Name
property in
the Properties window. Use the
Caption
property to change the text on the
title bar.
The easiest and least error-prone method of adding a control to a form is to
select the control in the Toolbox and then click the area of the form where
you want the control to appear. The mouse cursor points to the upper-left
corner of the control. The cross cursor makes it easy to place the control
accurately. You might need to organize the
controls after you add them
because it isn’t easy to add the controls precisely. See the upcoming
“Making your form pretty” section for details.
Another method for adding controls to a form is to drag the control from
the Toolbox and then drop it on the form. The reason why this method is
less precise is that you don’t always see the control’s location. The IDE dis-
plays a box showing the outline of the control, so you do see the basic layout
when using this method.
OptionButton
ListBox
TextBox
ComboBox
Label
CheckBox
Select
Objects
SpinButton
MultiPage
TabStrip
ScrollBar
Image
CommandButton
ToggleButton
RefEdit
Frame
Figure 7-2:
VBA pro-
vides a
useful
set of
controls that
meet most
needs.
159
Chapter 7: Interacting with the User
12_046500 ch07.qxp 12/5/06 5:35 PM Page 159
Understanding the two parts of a form
Forms, and the controls they hold, provide the visual portion of a user inter-
face. This is the part of the form interface that the user sees, but it isn’t much
good without some code. You need code to tell VBA what to do when the
user interacts with the form.
Any user interaction is an
event,
and the code
you build is an
event handler.
Consequently, when you create a form, you
work with two windows. The first is a UserForm window, where you address
the visual elements;
the second is the Code window, where you add code to
handle user events, such as clicking a command button.
When you add a new form to an application, the Visual Basic Editor opens
the UserForm window immediately and displays the Toolbox so that you can
add controls. However, it doesn’t display the Code window. You can use any
of the following techniques to open the Code window:
Choose View
➪
Code or press F7.
Right-click the UserForm entry in Project
Explorer and choose View
Code from the context menu.
Right-click the control you want to work with and choose View Code from
the context menu.
The last option on the list not only displays the Code window but also creates
a handler for the default event for that control. All standard VBA controls have
a default event. For example, the
CommandButton
control uses the
Click
event, which occurs when the user clicks the command button. Even the form
has
a default event,
Click
, so this last option always results in the creation
of an event handler. The default event depends on the action that the user is
most likely to perform. See the later “Handling form events” section of this
chapter for more information about events.
Using the Label control to display text
You use the
Label
control to display text onscreen. A
Label
is for informa-
tional purposes. Although you
can copy the content of a
Label
, you can’t
change it directly. The most common use for labels is to identify other controls
and to provide accelerator key access to them. (See the upcoming “Modifying
the form and control properties” section of this chapter for details on adding
an accelerator key.) Labels also act as output for read-only text.
Figure 7-3 shows a simple form with two labels. The first label has
Message
Text
in the
Caption
property. The label uses
M
as the
Accelerator
prop-
erty value, so the M in Message Text is underlined. When you press Alt+M,
VBA selects the
TextBox
control associated with the first label.
160
Do'stlaringiz bilan baham: