Properties describe objects. Methods cause an object to do something. Events are what happens when an object does something.
Every object, such as a form or
control, has a set of properties that describe it. Although this set isn't
identical for all objects, some properties--such as those listed in Table 1--are common to most controls. You can see every property for a given
control by looking at the Properties window in the IDE.
TABLE 1 Common Properties of Visual Basic Controls
Property
|
Description
|
Left
|
The position of the left side of a
control with respect to its container
|
Top
|
The position of the top of a
control with respect to its container
|
Height
|
A control's height
|
Width
|
A control's width
|
Name
|
The string value used to refer to
a control
|
Enabled
|
The Boolean (True/False) value
that determines whether users can manipulate the control
|
Visible
|
The Boolean (True/False) value
that determines whether users can see the control
|
Another important property to
consider is BorderStyle, which determines the window elements (title
bar, Maximize and Minimize buttons, and so forth) a form will have. Table 2
summarizes the six BorderStyle settings;
TABLE 2 The Six Settings of the BorderStyle Property
Setting
|
Description
|
0-None
|
No borders, no title bar, not
movable. Use this as a backdrop for a splash screen.
|
Setting
|
Description
|
1-Fixed Single
|
Not sizable by dragging borders
but can have Maximize and Minimize buttons. Use this for any fixed-size
window for which you want a button to appear in the taskbar.
|
2-Sizable (default)
|
Sizable by dragging borders and by
using Maximize and Minimize buttons. Use this for typical programs.
|
3-Fixed Dialog
|
Not sizable and no
Maximize/Minimize buttons. Use this for simple forms such as a password
dialog.
|
4-Fixed ToolWindow
|
Similar to 3-Fixed Dialog except
that the title bar is shorter and the title bar font and Close button are
correspondingly smaller. Use this for floating toolbars.
|
5-Sizable ToolWindow
|
Similar to a 4-Fixed ToolWindow
except that it's sizable by dragging the border. Use this for windows such as
the Visual Basic Properties window.
|
Methods are blocks of code designed
into a control that tell the control how to do things, such as move to another
location on a form. Just as with properties, not all controls have the same
methods, although some common methods do exist, as shown in Table 3.
TABLE 3 Common Methods of Visual Basic Controls
Method
|
Use
|
Move
|
Changes an object's position in
response to a code request
|
Drag
|
Handles the execution of a
drag-and-drop operation by the user
|
SetFocus
|
Gives focus to the object
specified in the method call
|
ZOrder
|
Determines the order in which
multiple objects appear onscreen
|
Events are what happen in and around
your program. For example, when a user clicks a button, many events occur: The
mouse button is pressed, the CommandButton in your program is clicked, and then
the mouse button is released. These three things correspond to the MouseDown
event, the Click event, and the MouseUp event. During this process, the
GotFocus event for the CommandButton and the LostFocus event for whichever object
previously held the focus also occur.
Again, not all controls have the
same events, but some events are shared by many controls (see Table 4). These
events occur as a result of some specific user action, such as moving the
mouse, pressing a key on the keyboard, or clicking a text box. These types of
events are user-initiated events and are what you will write code for
most often.
Using GotFocus and LostFocus
The GotFocus and LostFocus events
relate to most other events because they occur whenever a new control becomes
active to receive user input. This makes GotFocus and LostFocus useful for data
validation, the process of making sure that data is in the proper format for
your program. Be careful, though! Improperly coding these two events can cause
your program to begin an endless loop, which will cause your program to stop
responding.
TABLE 4 Common Events of Visual Basic Controls
Event
|
Occurrence
|
Change
|
The user modifies text in a combo
box or text box.
|
Click
|
The user clicks the primary mouse
button on an object.
|
DblClick
|
The user double-clicks the primary
mouse button on an object.
|
DragDrop
|
The user drags an object to
another location.
|
DragOver
|
The user drags an object over
another control.
|
GotFocus
|
An object receives focus.
|
KeyDown
|
The user presses a keyboard key
while an object has focus.
|
KeyPress
|
The user presses and releases a
keyboard key while an object has focus.
|
KeyUp
|
The user releases a keyboard key
while an object has focus.
|
Event
|
Occurrence
|
LostFocus
|
An object loses focus.
|
MouseDown
|
The user presses any mouse button
while the mouse pointer is over an object.
|
MouseMove
|
The user moves the mouse pointer
over an object.
|
MouseUp
|
The user releases any mouse button
while the mouse pointer is over an object.
|