Introduction To Programming Using Visual Basic Lesson 8: More Controls
Introduction To Programming Using Visual Basic Lesson 8: More Controls
Covering
Hour 10: List Boxes and Data Lists
Hour 11: Additional Controls
Lecture
List Boxes
Combo Boxes
Check Boxes
- Check boxes allow the user to select or deselect a single option. For example,
in a font dialog box there might be check boxes to turn Bold or Italics on or
off.
- The three letter prefix for check boxes is chk. For example, a check box used
for bold face may be called chkBold.
- Important Properties:
- Caption – Much like the caption of a label or command button, this
property sets what the user will see next to the check box.
- Value – This property corresponds to whether the check box has a
check inside it or not. Value is True (or 1) when the check box has a
check, and False (or 0) when not checked.
Option Buttons
Part 1
In this lab we will create a program which will allow us to format text inside a text box.
Create these controls and place them according to the picture below. Note that the top
most control is a text box.
Make sure you type Option Explicit in the General Declaration area. Any variables that
need to be declared should be declared in this area. Add them as you need them during
your programming.
The form load event is going to be very important in this program. Much of the work of
this program will be done in this event.
Create the click event for the Apply button. This click event should determine the font,
font size, any attributes such as bolding or italics, and the color from the appropriate
controls on the form. It should them proceed to set the properties of the text box
according to these settings. The appropriate properties of the text box that should be used
are: Font, FontSize, FontBold, FontItalic, FontUnderline, ForeColor.
Create a click event for the Exit button that will use a message box to ask the user if they
really want to exit. If the user selects yes, then the program should exit. If the user selects
no, then program should keep running.
Part 2
Create a Copy of the Project
1. Make sure you save your work before beginning this section.
2. Make a copy of the project by selecting Save Project As and changing the name of
the project to TextFormat2.
3. Make a copy of the form by selecting Save Form As and changing the form name
to TextFormat2
In this section we are going to remove the apply button hand have all of the formatting
options take effect immediately upon changing the options. For example, as you select a
font, you should see the font in the text box change immediately.
Create click events for all the objects that effect the text box formatting: lstFont,
cmbFontSize, chkBold, chkItalics, chkUnderline, optBlack, optBlue, and optRed. Inside
the click events, set only the appropriate formatting property of the text box. (i.e. If the
user clicks the check box for Italics, then set the FontItalic property of the text box
according to the value of the check box.
Part 3
Make all the Fonts Available
Our program only makes a few fonts available for selection. If we want to generate a list
of all the available fonts we can use the Screen object. The screen object has two methods
that can help us. The first is FontCount. This property evaluates to the total number of
available fonts. For example you can type the line:
The second is the Fonts method. This method takes the font index as an argument and
evaluates to the string representing the font. For example:
Using this information, change the lines that add the few fonts to the list box to a loop
that will add every available font to the list box. Keep in mind that the argument passed
to the Fonts method is a zero based index. This means if there are five fonts, then their
index numbers are 0, 1, 2, 3, and 4.