CIS 230, Visual Basic
Ch. 2 Notes, page 1
Objective #1: Use text boxes, frames, check boxes, option buttons, images,
shapes, and lines effectively.
- A program should take advantage of the visual nature of VB programs. It is easy to use
text boxes, frames, check boxes, shapes, lines, etc. but one must be careful to make
user-friendliness a priority. Look closely at commercial Windows applications that you use
on your desktop PC to see how professional developers use such controls.
- A text box should be used if the user is to enter or modify text. It
usually has a white background and can contain a large amount of text if its MultiLine
property is set to True. Labels on the other hand should be used if the user is not meant
to enter or change text. For this reason, it is sometimes not necessary to rename labels
with the lbl prefix if the contents and properties of the label are to remain unchanged
throughout the program's execution. Text boxes should be renamed with purposeful names
that begin with the txt prefix.
- A string literal is a set of characters that are enclosed in double
quotes. For example, "Visual Basic" is a string literal that is contains 12
characters (counting the blank space.) Sometimes, we call string literals simply
"strings".
- To assign a string literal to the Text property of a text box during run-time, you can
use a statement like, txtUserName.Text = "John Doe"
- To assign the contents of one text box's Text property to another's, you can use a
statement like, txtUserName.Text = txtProgrammerName.Text . In
this case, whatever was contained in txtProgrammerName is now contained in txtUserName,
also. What previously was stored within txtUserName was overwritten and lost. This is an
example of an assignment statement which evaluates from right to left (as
in most computer languages.)
- By the way, you must first set a text
box's Multiline property to True to be able to set its Alignment
property.
- Frames are meant to group related objects on a form window. A frame is
indicated by a rectangle. You should first create the frame by single-clicking its Tool
icon and then drawing it on the form window with your mouse. Then, you should create the
objects within its boundaries to ensure that VB treats them as being inside the frame.
- Option buttons (sometimes called radio buttons) are round circles that
can be selected by the user. Only one option button though can be chosen with any set of
related ones. This aspect distinguishes them from check boxes (see below) which are
squares. Related option buttons should be placed within a frame so that they work
correctly. Option buttons require the user to select one with any group but if your form
has more than one set of option buttons, your code may have a logical error if you are not
careful to use a frame around each set. The VB naming prefix for option buttons is opt.
- Check boxes are very useful for obtaining user input. A user can select
any number of check boxes within a group. The VB naming prefix for check boxes is chk.
- Images can enhance a program visually. Visual Basic supports a number
of types of graphics and images. Use the image control to place images on your form. Be
sure though to set the image's Picture property correctly so that it is displayed
properly. While there are dozens or hundreds of types of computer graphics and images,
Visual Basic allows you to use images with the following extensions: .bmp,
.wmf, .ico, .dib., .gif, .jpg, .emf., or .cur. Note that .gif and .jpg are
common on the World Wide Web, so you can download those types of graphics from a Web page
and then use them within your VB program (as long as you don't break copyright laws.) You
should probably locate these images within the folder that you created for your VB
project.
- Shape and line controls can be used to make your form
window look appealing. The naming prefixes for these objects are .shp and .lin,
respectively.
Objective #2: Set the Appearance property to make controls appear flat or
three-dimensional.
- Experiment with the Appearance property in various types of controls to
achieve different effects.
Objective #3: Select multiple controls and move them, align them, and set
common properties.
- If you have many controls on a form window and would like to move them all at once,
simply choose the Pointer tool and drag to make a rectangle around them. This will select
all of them at once. Then you can drag the whole set by clicking within any of the
selected objects. You can effect the common properties of the multiply selected controls
by making changes to the Property window. This allows you to build consistency into your
VB program and it saves time.
- You can also multiply select numerous controls by holding down the Shift or Control
(Ctrl) key while you select each one.
- There are a number of alignment options under the Format/Align menu options. You must
first multiply select the controls that wish to align though.
- You can also align controls by setting their Left, Top, and
Width properties. The measurements that you use there are twips, where one twip
is 1/1,440 of an inch. While you could set an object's Width property to 1,440 twips, it
still may not appear to be exactly one inch on your screen due to differences in monitor
resolution.
- At any time, you can right-click on the background area of the form window and
choose Lock Controls from the shortcut menu. This will preserve the arrangement of
controls that you tediously aligned or placed.
Objective #4: Make your projects easy for the user to understand and operate by
defining access keys, setting a default and a cancel button, controlling the tab sequence,
resetting the focus during program execution, and causing ToolTips to appear.
- You should always assume that your program is going to be used by
"knuckleheads", that is novice computer users. Giving these users multiple ways
to accomplish tasks in your program will provide for a more user-friendly experience.
Strive to make directions and prompt messages clear.
- Set up access keys (keyboard shortcuts) to make it possible for a user
to keep his hands on the keyboard and to avoid having to reach for the mouse. Some people
prefer using the keyboard in this way even if you don't. For example, to save an active
document in many Windows applications, a user can type Ctrl + S. This keyboard shortcut
has become a standard in all applications. To use access keys in VB, you must precede one
of the letters in an object's Caption property. The letter that you chose will be
underlined in the form window. This will allow the user to select that object by pressing
Alt + [the letter you chose]. Never use the same access key for two objects that would
appear on the form window simultaneously. This will confuse the user and possibly crash
your program. Follow access key standards that you see in commercial Windows applications.
- You can set one command button on a form window to be the default button. Simply set its
Default property to True. This will cause that button to be selected when
the user presses the Enter key. It also causes the button to have a darker outline than
other command buttons.
- Most DOS and Windows software uses the Esc (escape) key in the upper-left corner of the
keyboard to allow the user to "back out of" or cancel the current screen or
window. I call this the "escape hatch". If you have a Cancel command button on
your form, it is definitely a good idea to set its Cancel property to
True, causing it to be selected if the user typed the Esc key.
- At any time, one of the controls on a form has the "focus".
This control is often noted with a dotted line or other indication that it has the focus.
If the user presses the Enter key, often it is the control with the focus that becomes
selected. When the user presses the Tab key, the focus changes to another control. By
carefully manipulating the TabIndex property of various controls on your
form, you can make your program much more logical and user-friendly. When the program
begins, the control with the lowest TabIndex (usually starts at 0) has the focus.
- You can reset the focus to one control by using a statement like,
txtUserName.SetFocus
. This would cause the text box named txtUserName to receive the focus no matter
what it's TabIndex property might be.
- ToolTips are the little pop-up windows that appear when you move the mouse over some
objects in various software applications. Your VB program will be more user-friendly if
you set the ToolTips property of various controls to text words or phrases that indicate
their purpose. By leaving the ToolTip property empty, the object will not have a ToolTip.
Objective #5: Clear the contents of text boxes and labels.
- Programmers refer to "" as the empty string. It is sometimes
called the null string. That is, by typing double quotes with no spaces
in between, you are indicating an empty string literal. To clear the contents of a text
box named txtUserName, you would simply use the statement,
txtUserName.Text = "" . To clear a label's
Caption property, you might use,
lblName.Caption = "" . Of course, these statements would
work at run-time but you could also clear these properties in the Properties window during
design-time.
Objective #6: Change font attributes, such as bold, italic, underline, size,
and color, during program execution.
- One object in VB is unlike the others. It is the Font object. A font
object cannot be created from the Toolbox. Rather, you must choose the Font property
within text boxes, labels, etc. to manipulate the properties of the Font object.
- You have much control over the appearance of an object's font during run-time or
design-time. A statement like,
txtPlace.Font.Bold = True , would make the text within a text box
appear bold. You could also achieve this effect during design time simply by typing on the
Properties settings in the text box's Font property.
- Experiment with the Font object to see what kinds of appearances text can have in VB.
- Answer the Feedback 2.2 questions on p. 67 at this time.
- VB has 8 built-in constant colors named vbBlack, vbRed, vbGreen,
vbYellow, vbBlue, vbMagenta, vbCyan, and vbWhite that may be used within assignment
statements. You will see a palette of color choices within the Properties settings of
properties such as ForeColor, FillColor, BackColor, etc. These color choices are denoted
with hexadecimal numbers that represent the standard range of computer RGB colors.
Do Feedback 2.2 #1-3 on p. 67 at this time.
Objective #7: Code multiple statements for one control using the With and End
With statements.
Chart of controls and their default
properties
Control |
Default Property |
Check box |
Value |
Combo box |
Text |
Command button |
Value |
Frame |
Caption |
Horizontal scroll bar |
Value |
Image |
Picture |
Label |
Caption |
Line |
Visible |
List box |
Text |
Menu |
Enabled |
Option button |
Value |
Picture box |
Picture |
Shape |
Shape |
Text box |
Text |
Timer |
Enabled |
Vertical scroll bar |
Value |
Objective #8: Concatenate (join) strings of text.
- When two string literals (that is, characters enclosed in double
quotes) are joined together without placing any spaces between them, programmers call this
concatenation. To concatenate two strings in VB, you should use the ampersand
(&) symbol.
- Example: To concatenate the text within two separate text boxes and
place the concatenated text into a label, you could use this statement,
lblUserName = txtFirstName &
txtSecondName
- Since the ampersand is used to concatenate text, it is called the concatenation
operator. While VB sometimes allows the plus (+) symbol to be used for
concatenation, you should avoid this since it is easy to mistake it for the mathematics
operation of addition.
Objective #9: Make a control visible or invisible by setting its Visible
property.
- You can make an object disappear during run-time with a statement like,
txtHeading.Visible =
False
- You can certainly hide an object during design-time by setting its Visible
property to False and then make it appear during program execution by setting its
Visible property to True as above.
Do the Review Questions #1-17 on pp. 79, 80 at
this time.
CIS 230 Home Page | Mr. Minich's Education Home Page | Minich.com Web Design