This unit explains all about the format of Visual Basic programs. Unlike most programming languages, the Visual Basic language contains text commands as well as graphical controls. The text commands interact with the controls to produce the
output of
the program and the interactions with the user.
This unit concentrates on the graphical controls that you put in Visual Basic programs. As you read further into this book, you will gain a deep understanding of how the commands work.
Some of the Visual Basic commands are easy, and some are complex.
This unit concentrates on explaining the contents of programs. It shows you how to load and run Visual Basic programs.
Note: This is a hands-on unit. You will use Visual
Basic as you progress through this unit to learn how each of Visual Basic's graphical tools work.
Concept: Once you start Visual Basic, you can create a program. Once you create a program, you can save that program and load and run it later. This unit gives you practice in loading and running a program that
is stored on the disk that comes with this book.
Definition: Users perform I/O with controls.
This book's disk
comes with a program that demonstrates several of the most important controls in Visual Basic. A control handles much of the user's input and output, often called I/O. In other words, when someone (the user) runs the program that
you (the
programmer) creates, the user must interact with the program by providing answers to questions and selecting options displayed on the screen.
Remember that the Toolbox window is where you get the controls that you place on the Form window. The
Form window is the background of the application that you are creating. Figure 3.1 shows where the controls appear on the Toolbox window.
Figure 3.1. The controls that your program needs are available on the
Toolbox window.
The Project File: A Visual Basic program rarely resides in a single file. Most of the time, it takes several files to describe a single Visual Basic application. It often helps to separate the pieces of a Windows program into multiple files.
For example, there is almost always a form file. The form file holds the contents of the Form window that you create as well as all the code that manipulates the objects on the Form window. You might also write additional code that you want to store in a separate file. If your the application requires special controls that do not regularly appear on the Visual Basic Toolbox window, you must add those control files to the application, too.
The Project window contains a list of every file in the application. By default, the Visual Basic Primer disk always adds three files to every application's Project window: A form file with the default name FORM1.FRM and two special Visual Basic controls called GRID.VBX (more commonly known as the grid control) and OLECLIEN.VBX (called the OLE control). For now, dont worry about the special controls.
The Project window's contents are described in yet another file, which is called the project file. Every application has a project file, which always ends with a .MAK extension. When you want to load and run an application, you load its project file; Visual Basic ensures that all files in the project are loaded as well.
There is a program on this book's disk described in the
project file called CONTROLS.MAK. Follow these steps to load CONTROLS.MAK:
Figure 3.2.
The Form window is often very crowded.
The Form window works like an artist's easel of paper. Actually, the Form window works more like an artist's easel of one or more transparencies with something differently drawn on each
transparency. CONTROLS.MAK contains several controls placed on top
of one other. The program looks like a mess.
When the user runs the program, he sees the Form window. What the user sees, however, does not always look like the Form window when
the program is first loaded. In other words, you are now looking at all the pieces of the program piled on top of each
other. When the user you, in this caseruns the program, the program ensures that those pieces appear in a logical
order and do not overlap.
Before you or anyone else can run a program from within the Visual Basic environment, you must load the program through the File Open command, just as you have done. Once you load a program, you have three ways to run
it:
Use one of these three methods to run the program. You will see the more organized and less cluttered screen shown in Figure 3.3. The program's initializing
instructions cleaned up the mess that you originally saw in the Form window. As you can see, the
Form window of the Visual Basic environment becomes the application's background (minus the grid of dots in the background).
Figure 3.3. The running program is much cleaner.
This program demonstrates each of the primary Visual Basic controls so that you can familiarize yourself with using them before you place them in your own
applications.
Tip: Notice that the program contains an Exit command button in the lower-right corner. All of this book's applications offer an Exit command button so that you can terminate the program whenever you want. When you create your own programs, always give your user a way to exit the program.
Review: Before you can
run a Visual Basic program, you must load it. Generally, you load the project file, which always ends with a .MAK extension. Loading the project causes Visual Basic to load all the files
related to the application. Without a mechanism such as a project
file, you would have to keep track of all the files related to every application and load each of them individually whenever you wanted to run a Visual Basic program. Once Visual Basic loads
all the files related to the project, you can run the program
and see what the program does.
Concept: The label control is
one of the simplest controls to work with. With the label control, you can add descriptive text to the form in any location by using different styles and sizes of fonts.
The label control, appearing on the Toolbox window with a capital letter
A, holds text. The user sees the text on the resulting application. The title of the CONTROLS.MAK application, Have fun with controls! is placed on the form with the label
control.
As you will see as you progress through this book, you
can set several property values as you place controls. You manage the size of the control and the way the control looks. You can specify a large font size or a small font size, a font style based on
any available font style in your Windows system, whether
you want the font boldfaced, italicized, or struck through with the strikethrough font style, where the text has a straight line drawn through it.
You can draw a box around the label and shade its background any color that you want. Look again at
the running program. You can see a command button labeled Next Control. Click this command button with the mouse or press Alt+C. Two new labels will
appear in the middle of the screen, as shown in Figure 3.4.
Figure
3.4. Two additional label controls appear in the center of the form.
Tip: If a command button contains an underlined characterfor example, the Next Control command button at the right edge of the program window in Figure 3.4the underlined character represents the shortcut access keystroke. You can combine the Alt key with the shortcut access key to trigger a press of the command button.
Warning: The user cannot change the text on a label directly. Through code, you can change the label's text in response to a user's action when the need arises. Generally, however, you will set the text within a label when you design the program.
Review: The label control is one of the
easiest controls to add to your applications. Whenever you need to display text within a title or a description for another control, the label control enables you to
display the text with various font sizes and styles.
Concept: Unlike the label control, the user can change values within a text box control. You can
get answers from the user by using text box controls.
When you click the Next Control command button on the running program, the labels that you displayed earlier disappear and a text box appears in their place, as shown in Figure 3.5.
Figure 3.5. The user can change the text within a text box.
Tip: If you display a text box that has no initial text within it, the user can type text in the text box control in response to a question that you ask. Sometimes, however, you will display initial text within the text box that the user can use as a default value, changing the text only if the default is not the needed value.
Click the mouse cursor anywhere within the Change this! text box. Type new text in the box. Type lots of text in the text box. The text scrolls to the right to accommodate the new text. You can use
the arrow keys to move the cursor back and forth within
the text box. Also, the Ins and Del keys work just as they do in a word processor. With these two keys, you can insert and delete text from the text box.
The text box controlthe
control with the lowercase ab enclosed in a boxenables you to set an initial value and to control how the text's font style and size appear when the user sees or enters text in the text box. You can control whether
the text box contains horizontal
and vertical scroll bars so that the user can scroll through the text with scroll bars or with the regular arrow keys.
Review: When the user must enter new text or change existing text, use the text box
control to place an area on the screen for the user's keystrokes. The text box control works just like a mini word processor.
Concept: You have seen command buttons in almost every Windows program, including Visual Basic and the currently-running CONTROLS.MAK application. Command buttons give users
push-button access to events that
you place within an application.
A command button appears on the screen just as push buttons appear on a VCR or on your keyboard. The CONTROLS.MAK program contains two command buttons that stay on the screen at
all times: The Next Control command button and the Exit command button. As
you already know, you can trigger a command button's press with a mouse click or with a shortcut access keystroke.
Press the Next Control command button now to see a
third command button replace the text box control in the middle of the screen of the running CONTROLS.MAK application. Figure 3.6 shows the screen after the command button is displayed.
Figure 3.6. A command button
controlled by the program.
Virtually anything can happen when the user clicks a command button. You, the programmer, control exactly what happens. Go ahead and press the CONTROLS.MAK command button. Lookand listen
towhat happens. The computer beeps, and the command
buttons caption changes from Press Me to Once Again. Press the command button again to hear the beep once more and to restore the command button to its original Press Me state.
You
set the command button's caption usually when that you design and write a program. As you can see, you can write the program so that the command button's caption changes when the user clicks the button.
Review:
A Windows application without command buttons is like day without night. (Okay, that's exaggerating.) Command buttons supply push button access to events that you want the user to trigger.
Concept: Check box controls offer multiple-choice values from which the user can select. Once the user
selects one or more check boxes, your program can analyze the selected check boxes and make decisions based
on those responses.
Click the Next Control command button to see the list of three check box values shown in Figure 3.7. The check boxes
offer the user a way to select one or more values from a list of values that you display.
Figure 3.7. Two of the three check boxes are selected.
With the mouse, click the first and last check
boxes on the running CONTROLS.MAK application, as shown in Figure 3.7. When you select a check box, the boxes to the left of the descriptions fills with an X.
Once you click these two controls, the two values are said to be selected, and
the middle value is unselected or not selected. You can deselect a check box by clicking it a second time. Click the Pears check box to deselect
that check box. The X leaves the check box when you deselect the box.
Tip: You can click not only on the box but anywhere within a text box's description. Therefore, to select or deselect the Pears check box, you can click the mouse cursor over the box next to Pears or click on the word Pears.
There is also a way to select check boxes without the mouse. The user can press the Tab key until the check box description highlights. To select it, the user presses
the Spacebar.
Review: Check boxes give your users multiple-choice access to choices that they need to make. Users select and deselect check boxes with the mouse or keyboard. Once the user selects all the
check boxes needed,
a command button keypress can signal to the running application that the user is finished. Your program then can check which values the user selected.
Concept: Unlike check boxes, option buttons give your users a list from which to choose, but they can select exactly one
option out of the list.
Often, the option button controls are known as mutually-exclusive controls. Unless you group sets of option buttons in frames (described later in this unit), the user can select one and only one option button
at a time.
Click the Next Control command button to see the CONTROLS.MAK option buttons on your screen like the ones shown in Figure 3.8. Initially, no options are selected.
Figure 3.8. Option buttons work
almost like mutually-exclusive check boxes.
With the mouse, click one of the option buttons. Click another option button. At once, Visual Basic deselects the first option button and selects the one you just clicked. Select another
option button to change the selected option once again. As you can
see, Visual Basic ensures that you can select only one option at a time. Instead of check boxes, which permit multiple selections, you would display option buttons for the user whenever he
can make only one choice out of several.
Tip: Think ahead when you use option buttons. For example, you can select an initial option button for the user, through code or through the Properties window, when you write the program. By selecting an initial option button, you ensure that the user knows the best choice in a given situation, assuming there is a good default value that you can select.
Review: The option buttons work like check box controls except that, whereas the check box controls permit multiple selections, the user can select at most one option button at a
time.
You've Been Framed
Concept: The frame control enables you to group items together on a form. The group works almost like a miniform within the form.
Although the user can
select only one option button at a time, you can set up groups of option buttons on the same form. The user can then select one option button at a time from within each frame.
Definition: A frame is a box in which you can place control groups.
You must always enclose the group within a frame by using the frame control. If you press the Next
Control button again, you will see a frame appear in the middle of the form, as shown in Figure 3.9. You can place any controls in a frame, not just
option buttons as in the figure.
Figure 3.9. A frame with a command
button and two option button controls.
If three frames of option button groups appeared on the form, you could select a maximum of three option buttons on the formone in each framed set.
Note: To keep things simple at this point, only one control is discussed at a time. Only one set of option buttons currently appears on your form. There is no special reason to group them together except to show what a frame is.
Review: By framing objects together with the frame control, you can set up groups of controls that work together as if they were each on
their own miniform within the larger Form window.
Review: A
dropdown combo list is one of three kinds of lists that you can provide for your user. The dropdown list saves room on the screen by consuming only a single line on the form until the user opens the
list to display the rest of the items in it.
The combo box control actually turns into two different kinds of controls on the form, depending on how you set up the combo box. The two kinds of combo boxes are
Press Next Control to see the combo dropdown box on the screen. As so often is the case, a command button appears next to the combo dropdown box. (See how you can resize every control, including command buttons, so that the controls
consume exactly as
much screen space as needed?)
A list of items is stored in this dropdown combo box. To see the list, click the down arrow at the right of the empty dropdown combo box. You will see the list of electronic gear shown in Figure
3.10. Click the down arrow again. The list folds back up
leaving the screen clear of the extra clutter.
Figure 3.10. After opening the dropdown combo box control.
Tip: When screen real estate space is precious, use a dropdown combo box control when you must offer a list of items to the user. The user can display the list when he needs to see what is in the list, and he can restore the list to its normal state when he is finished.
The blank space at the top of the list is for the user to add additional items to the dropdown combo box. You will want to add a command
button next to the combo box, as in CONTROLS.MAK, so that the user can add new items after typing them. Add two
additional items by following these steps:
Note: Later, you will learn how Visual Basic can keep the items sorted alphabeticallyyou do not have to write any code to sort the items.
The items remain in the dropdown combo box list until you quit the program. Throughout this book, you will learn ways to save the items in the list and to
initialize the lists with values using code.
Review: The dropdown combo box control offers a handy way for the user to view and add items to a list. The dropdown combo box gives you the advantage of
listing items for the user without taking up screen
space that is needed for other things. Your user can display the entire list by clicking the down arrow.
Concept: The simple combo box control does exactly the same thing as the dropdown combo box except that the simple combo box is always
displayed in its dropdown form. In other words, if screen space is
not a problem, you might want to use a simple combo box to display and collect values by using a list that does not require the user's extra keypress to open the list.
Press the Next Control command button to see what a simple combo box looks like. As you can see, there is no difference between a simple combo box and a dropdown combo box except that the simple combo box is always open.
If the simple combo
box is not deep enough to display all the items in the list, Visual Basic adds a vertical scroll bar to the list so that the user can scroll through the list and view the items. As with the dropdown combo box, the user can add items
to the simple combo
box by typing the new value and clicking a command button that you set up.
Type the value Rugby and click the Add command button. Scroll to the bottom of the listyou can use the Page Down key to scrollto see the new sport's
name.
Note: There is a third kind of combo box, called a dropdown list box control, that I will not cover here. The dropdown list box offers nothing new that you cannot get by using the other combo boxes or list box controls. Discussing it would only add confusion at this point.
Review: Simple combo box controls provide an
easier way to display and collect list values. The user does not have to understand how to open a simple box controlit is always open.
Concept: List boxes are easy to understand compared to the two combo box controls. If you want users to select from a choice of options that you have supplied and you
want to prevent them from adding additional
items to the list, use list boxes.
Figure 3.11 shows how your screen looks after you click the Next Control command button again. Later you will learn how to capture the item or items that the user
selects from a list box.
Figure 3.11. Selecting from a list box.
Warning: Check boxes and option buttons give users a chance to select from options, but you should not use check boxes or option buttons when there are many options to choose from. The scrolling list box and combo box controls use screen space more efficiently.
Click over one of the list box items with the mouse. Visual Basic highlights the item. Through programming, you will know when users selects an item, and you can analyze what they select. Click another item to select another choice.
If the
application requires multiple selections, you can set up the list box for multiple selections. The list box control inside the CONTROL.MAK application permits multiple selections. Hold down the Ctrl, Alt, or Shift key and click one or two
additional
items. Notice that Visual Basic highlights all the items. You can control whether users can select only one or more than one item from a list box. As with combo box lists, you can request that Visual Basic sort the items in the list box
alphabetically.
If you want, you can step through all the controls again by clicking the Next Control command button. You have now seen all the controls offered by the CONTROLS.MAK application. Although you will learn about more controls later in this book, this
unit
has described the primary controls that all Visual Basic programmers should master. Be sure that you know all the names of the controls. Study Figure 3.1 so that you know where each control is located on the Toolbox window.
Warning: Not every Windows program that you write will contain all these controls. As a matter of fact, no Windows program should contain all these controls. The program would be too busy and would require too many different kinds of responses from the user.
Review: List boxes are controls that give the users choices from which they can select.
Your application is responsible for initializing list boxes with values. The user cannot add items to a list box.
Suppose that you are writing an application that offers users a list of three values from which they can select at most one of the values. Which control is the most
appropriate?