Lecture 04
Lecture 04
MORE CONTROLS
VISUAL BASIC
in a frame or picture box) constitute one group. If you want to are measured in twips, which is a unit used in GUIs. Twips are
create additional option button groups, you must place some usually used instead of pixels because the screen resolution
of them inside frames or picture boxes. determines the size of the pixels. There are always 1440 twips in
All the option buttons inside any given frame constitute a an inch. If you put a control at 0,0 in a form by itself, it’s upper
separate group, as do all the option buttons inside a picture left corner will be at the upper left corner of the form. If you
box. When you create a separate group this way, always draw the put a control which belongs in a frame or picture box at 0,0, it’s
frame or picture box first, and then draw the option buttons on upper left corner will be at the upper left corner of the frame or
top of it. Figure 3.12 shows a form with two option button picture box. See? Another use of grouping controls is for radio
groups. buttons. Radio buttons are like surveys that ask you to check
one of the following. You see these in windows too. To have
A user can select only one option button in the group when
more than one radio button checked on a form, you separate
you draw option buttons in a frame.
them into logical groups and the user can enable one button
Drawing the frame first and then drawing each control on the from each group. We’ll get more into this concept when we get
frame allows you to move the frame and controls together. If into radio buttons, a little further down. Frames also have a .
you try to move existing controls onto a frame, the controls will caption property, which is like a label around the frame border
not move with the frame. for the group so the user knows what that group is
Containers for Controls representing.
While controls are independent objects, a certain parent and Using List Boxes and Combo Boxes
child relationship exists between forms and controls. To List boxes and combo boxes present a list of choices to the
understand the concept of containers, you need to understand user. By default, the choices are displayed vertically in a single
that all controls are children of the form on which they are column, although you can set up multiple columns as well. If
drawn. In fact, most controls support the read-only Parent the number of items exceeds what can be displayed in the
property, which returns the form on which a control is located. combo box or list box, scroll bars automatically appear on the
Being a child affects the placement of a control on the parent control. The user can then scroll up and down or left to right
form. The Left and Top properties of a control are relative to through the list.
the parent form, and controls cannot be moved outside the
A combo box control combines the features of a text box and a
boundaries of the parent. Moving a container moves the
list box. This control allows the user to select either by typing
controls as well, and the control’s position relative to the
text into the combo box or by selecting an item from its list.
container’s Left and Top properties does not change because the
control moves with the container.
Selecting or Disabling Option Buttons
An option button can be selected by:
• Clicking it at run time with the mouse.
• Tabbing to the option button group and then using the
arrow keys to select an option button within the group.
• Assigning its Value property to True in code:
• optChoice.Value = True
• Using a shortcut key specified in the caption of a label.
There are three styles of combo boxes:
• To make a button the default in an option button group, set
its Value property to True at design time. It remains selected 0 - Dropdown Combo 1 - Simple Combo 2 - Dropdown List
until a user selects a different option button or code changes A Simple combo box is not a dropdown box in the way that
it. there is no arrow on the right. The list of items is always visible.
To disable an option button, set its Enabled property to False. Create a combo box, set the style to 1, then set the height to
When the program is run it will appear dimmed, meaning that several lines tall. You’ll see that there is a big box there. It works
it is unavailable. basically the same as a dropdown combo box except that it
doesn’t drop down... it’s just always down.
Frame The Dropdown list is like the above two, with the exception
that the user can’t type stuff into a text entry at the top. It
You have also seen a lot of frames in the Windows operating works like the dropdown combo box in the way that it drops
system; in dialog boxes in particular. Frames are used to group a down.
series of controls together. Grouping controls can have a One thing about combo boxes is that their values can only be
number of effects. First of all, the coordinate system for a set at run time. You do this using the .additem method on the
control is based on it’s bounding control. Every control has an combo box.
x, y coordinate, x being the distance from the left and y being
In contrast to some other controls that contain a single value;
the distance from the top. The coordinate specifies the upper
for example the label’s Caption property or the text box’s Text
left corner of the control, and the width and height properties
or a collection of values. They have built-in methods for properties set the amount the scroll bar’s value iterated at run
adding, removing and retrieving values from their collections at time by the user. The .SmallChange applies to when the user
run time. To add several items to a list box named List1, the clicks an arrow, and the .LargeChange applies to when the
code would look like this: user clicks somewhere in the elevator. The standard is
List1.AddItem “Paris” .SmallChange=1, .LargeChange=10. The .Min and .Max
properties set the minimum and maximum values of the
List1.AddItem “New York”
scroll bar. On a horizontal scroll bar, the minimum value is
List1.AddItem “San Francisco” the leftmost position of the scroll bar. On a vertical scroll
List boxes and combo boxes are an effective way to present a bar, the minimum value is the top value of the scroll bar.
large number of choices to the user in a limited amount of As for events, the scroll bars have two main ones: _Change and
space. _Scroll. The _Change event occurs whenever the user 1) Clicks
The .list property applies to a list box or combo box and can be an arrow, 2) Clicks the elevator, or 3) Moves the handle, but not
used only at run time. You use it to set or read an item on the while the user is moving the handle. The _Scroll event occurrs
list. Here’s a new concept: every item on the list is assigned a only when the user is moving the handle. When one of these
number called an index when it is added. The first item in the events is called, the value of the scroll bar as obviously been
list has an index of 0, the second, 1, and so on. So using the changed
.list property, you could set the third item in the list like this
Controls That Display Pictures and Graphics
(assuming that the listbox’s name is lstPrime): lstPrime.List (2)
Because Windows is a graphical user interface, it’s important to
= “Fruit”.
have a way to display graphical images in your application’s
Another cool property unique to list boxes is the .columns interface. Visual Basic includes four controls that make it easy to
property. This defines the number of columns that can fit on work with graphics: the picture box control, the image control,
one “screen” of the list box. If the .columns property is set to 0 the shape control, and the line control.
(default) then when the number of items exce eds the maxi-
The image, shape and line controls are sometimes referred to as
mum length of the list box, a vertical scroll bar enables you to
“lightweight” graphical controls. They require less system
scroll down and see the rest. If the .columns property is a non-
resources and consequently display somewhat faster than the
zero, that many columns can be seen in the list box. There will
picture box control; they contain a subset of the properties,
not be a vertical scroll bar, but rather a horizontal scroll bar. Say
methods and events available in the picture box. Each is best
.columns is set to 3, and you have a lot of items in the list box.
suited for a particular purpose.
Three columns of items appear when you start it up, and a
horizontal scroll bar is at the bottom
To provide this feature Use this control
Using Scroll Bars as Input Devices
A container for other controls.
Although scroll bars are often tied to text boxes or windows, Picture box
you’ll sometimes see them used as input devices. Because these Displaying a picture.
Image control or picture box
controls can indicate the current position on a scale, scroll bar
controls can be used individually to control program input — Display ing a simple graphical element
Shape or line control
for example, to control the sound volume or to adjust the
colors in a picture. The HScrollBar (horizontal) and VScrollBar
(vertical) controls operate independently from other controls Working With the Picture Box Control
and have their own set of events, properties, and methods. The primary use for the picture box control is to display a
Scroll bar controls are not the same as the built-in scroll bars picture to the user. The actual picture that is displayed is
that are attached to text boxes, list boxes, combo boxes. determined by the Picture property. The Picture property
This scale is manipulated by either clicking an arrow on the scroll contains the file name (and optional path) for the picture file
bar (there is one at each end), clicking the elevator (the empty that you wish to display.
space on the scroll bar that is not a handle and is not an arrow), To display or replace a picture at run time, you can use the
or by moving the handle. Clicking the arrows increments the LoadPicture function to set the Picture property. You supply the
data a little, clicking the elevator usually increments the data a name (and optional path) for the picture and the LoadPicture
lot, and moving the handle increments the data at your own function handles the details of loading and displaying it:
pace. Using different forms of input makes your programs The picture box control has an AutoSize property that, when
more interesting. set to True, causes the picture box to resize automatically to
There are a couple properties and events worth mentioning for match the dimensions of its contents. Take extra care in
the scroll bars. designing your form if you plan on using a picture box with
• The most important property the value property there is a
the AutoSize enabled. The picture will resize without regard to
numerical value for every position the handle has on the other controls on the form, possibly causing unexpected results,
scroll bar. The value is changed by defining the LargeChange such as covering up other controls. It’s a good idea to test this
and .SmallChange properties, and bounded by the Min and by loading each of the pictures at design time.
VISUAL BASIC
controls. Like the frame control, you can draw other controls on Shape and line controls are useful for drawing graphical
top of the picture box. The contained controls move with the elements on the surface of a form. These controls don’t
picture box and their Top and Left properties will be relative to support any events; they are strictly for decorative purposes.
the picture box rather than the form. Several properties are provided to control the appearance of the
The picture box control has several methods that make it useful shape control. By setting the Shape property, it can be displayed
for other purposes. Think of the picture box as a blank canvas as a rectangle, square, oval, circle, rounded rectangle, or rounded
upon which you can paint, draw or print. A single control can square. The BorderColor and FillColor properties can be set to
be used to display text, graphics or even simple animation. change the color; the BorderStyle, BorderWidth, FillStyle, and
The Print method allows you to output text to the picture box DrawMode properties control how the shape is drawn.
control just as you would to a printer. Several font properties The line control is similar to the shape control but can only be
are available to control the characteristics of text output by the used to draw straight lines.
Print method; the Cls method can be used to erase the output.
Additional Controls
Circle, Line, Point and Pset methods may be used to draw Several other standard controls are included in the Visual Basic
graphics on the picture box. Properties such as DrawWidth, toolbox. Some controls are useful for working with large
FillColor, and FillStyle allow you to customize the appearance amounts of data contained in an external database. Other
of the graphics. controls can be used to access the Windows file system. Still
Animation can be created using the PaintPicture method by other controls defy categorization, but are useful nonetheless.
moving images within the picture control and rapidly changing You can also use ActiveX controls, previously called custom or
between several different images. OLE controls, in a Visual Basic application in the same way that
Lightweight Graphical Controls you use the standard controls. The Professional and Enterprise
The image, shape and line controls are considered to be editions of Visual Basic include several ActiveX controls as well
lightweight controls; that is, they support only a subset of the as the capability to build your own controls.
properties, methods, and events found in the picture box. Data Access Controls
Because of this, they typically require less system resources and In today’s business, most information is stored in one or more
load faster than the picture box control. central databases. Visual Basic includes several data access
Using Image Controls Instead of Picture Boxes controls for accessing most popular databases, including
The image control is similar to the picture box control but is Microsoft Access and SQL Server.
used only for displaying pictures. It doesn’t have the ability to • The ADO Data control is used to connect to a database.
act as a container for other controls, and it doesn’t support the Think of it as a pipeline between the database and the other
advanced methods of the picture box. controls on your form. Its properties, methods, and events
Pictures are loaded into the image control just as they are in the allow you to navigate and manipulate external data from
picture box: at design time, set the Picture property to a file within your own application.
name and path; at run time, use the LoadPicture function. • The DataList control is similar to the list box control. When
The sizing behavior of the image control differs from that of used in conjunction with an ADO Data control, it can be
the picture box. It has a Stretch property while the picture box automatically filled with a list of data from a field in an
has an AutoSize property. Setting the AutoSize property to True external database.
causes a picture box to resize to the dimensions of the picture; • The DataCombo control is like a combination of the
setting it to False causes the picture to be cropped (only a DataList control and a text box. The selected text in the text
portion of the picture is visible). When set to False (the box portion can be edited, with the changes appearing in the
default), the Stretch property of the image control causes it to underlying database.
resize to the dimensions of the picture. Setting the Stretch • The DataGrid control displays data in a grid or table. When
property to True causes the picture to resize to the size of the used in conjunction with an ADO Data control, it presents
image control, which may cause the picture to appear distorted. fully editable data from multiple fields in an external
Using an Image Control to Create Your Own Buttons database.
An image control also recognizes the Click event, so you can use • The Microsoft Hierarchical FlexGrid control is a unique
this control anywhere you’d use a command button. This is a control for presenting multiple views of data. Think of it as
convenient way to create a button with a picture instead of a a combination of a grid and a tree or outline control. At run
caption. Grouping several image controls together horizontally time, the user can rearrange columns and rows to provide
across the top of the screen — usually within a picture box — different views of the data.
allows you to create a toolbar in your application.
File System Controls
To create a border around the image control, set the BorderStyle Visual Basic includes three controls for adding file handling
property to 1-Fixed Single. capabilities to your application. These controls are normally
used together to provide a view of drives, directories and files;
they have special properties and events that tie them together.
provides a drop-down list of drives from which the user can • Name and describe the three styles of combo box
select.
• How can you make scroll bar appear on a list box or combo
box
• Explain the purpose of the ListIndex property and
• The DirListBox is similar to a list box control, but with the ListCount property
built-in capability of displaying a list of directories in the • When and how is information placed inside a list box or
currently selected drive. cmbo box
Notes
• The FileListBox control also looks like a list box with a list
of file names in a selected directory.