Lesson 4 Visual Basic Controls
Lesson 4 Visual Basic Controls
Lesson
Visual Basic controls are more than drawing objects. Controls have properties that vary
with the type of control, and events that determine when code associated with the
control is executed. Visual Basic, by default, has a number of commonly used controls
grouped in a toolbox.
Placing Controls
1. They can be selected and drawn using the mouse. This is the preferred method.
As you will find when you start working with controls, this method is more direct,
and requires less additional effort since you put it where you want it first time.
2. Double-clicking the control icon in the toolbox will place the control in the centre
of the form. It can then be dragged anywhere on the form and resized.
In this course, certain controls will be used quite often. Let's have a look at what they
do, and their most commonly used properties and events. Properties are values that
you can set to alter the appearance or behaviour of the control. Events are what occur
when the control is activated when the program is run. The first property listed is also
called the default property for the control. You will learn more about the default
property later in the course. Only a few of the many properties are listed below.
Scroll Bar. Scroll bars, both horizontal and vertical are used to set a value by
dragging or clicking. They can also be used as indicators.
Timer. The timer can be set to execute code at specified intervals. The timer is
not seen when the program is run.
Frame. Frames are used to group controls to improve the look of the graphical
interface.
When there are a number of controls on a form, it is often desirable to group them into
logical sets. This is part of the visual design process. Sets of related controls are
placed into a special box called a frame. Look at the next figure. The frames are the
dark blue boxes with labels in the top left corner. How many frames do you see?
When controls are placed on a frame by selecting and drawing directly on the frame
(rather than moving them on the frame after they are drawn), they become part of the
frame. If the frame is re-positioned on the form, the controls move with the frame.
Did you see 6 frames in the figure?
Consider the following examples. Click the play button to see the animation.
Animation Example 1: Controls placed on a frame by "double-clicking".
Animation Example 2: Controls placed on a frame by selecting and drawing.
Activity
The purpose of this activity is to develop capability with placing controls on a form,
setting their properties, and grouping controls in a frame.
In this activity you will add selected controls to the form from the toolbox, and set
some of their properties. To find the names of the controls in the toolbox, hold the
cursor over a control for a few seconds, and its name will appear.
Label control
Text Control
Click once (don't double-click) on the text control to select it. Then place the
cursor in the form, click and hold the left mouse button and drag it to the right
and down. When it reaches the size you want, release the left mouse button.
You can move it and resize it anytime.
Change the values in the following Text control properties
o (Name)
o Font
o ForeColor
o Text (change it to "" — that's 2 double quotes and it means that the text
box will be empty when the program is run)
Run the program. Are the effects suitable? If not stop the program and redo
them
Command Button
As you learned in the lesson, if you add a frame and then add controls to the frame,
they become part of the frame. If you add controls to a frame by dragging existing
controls from the form onto the frame, they do not become part of the frame.
Here's what happened. Each object is 'layered' above the previous one as
it is created. Since the form was created last, the objects, when placed
on it, go behind the form. To see them again, select the frame and send
it to the back by clicking on | Format | Order | Send to Back | (from the
menu), or CTRL K (Control key and k key on the keyboard)
Move the frame by selecting it and dragging it across the form. What
happened? Did the controls move with it?
Look at the following illustration. Use Visual Basic 6 to create an interface identical to
this illustration. As you add the controls, use the property window for each to set the
required property. You will need 3 frames and make sure you draw the controls on the
frames. As you can see from the illustration, frames can be drawn on frames.
Carefully observe the picture and determine the order in which frames and controls
need to be drawn. Background controls need to be drawn first.
Save the project and form to your course portfolio