Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
397 views

Introduction To Programming Using Visual Basic Lesson 8: More Controls

List boxes contain lists of data that can be viewed or selected. Combo boxes are a combination of a text box and a list box. The three letter prefix for a combo box is cmb.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
397 views

Introduction To Programming Using Visual Basic Lesson 8: More Controls

List boxes contain lists of data that can be viewed or selected. Combo boxes are a combination of a text box and a list box. The three letter prefix for a combo box is cmb.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

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

- List boxes contain lists of data that can be viewed or selected.


- Each item in the list has an index number which begins at 0 for the first item,
1 for the second item, etc.
- The three letter prefix for the name of a list box is lst. For example, a list of
people might be lstPeople
- Important Properties:
- ListCount – contains the number of items that are in the list
- ListIndex – contains the index number of the currently selected
item in the list. It can also be set in order to change the currently selected
item.
- Text – contains the text of the currently selected item. Can be set to
change the currently selected item.
- Sorted – set to true to have the list sorted alphabetically, or set to
false to keep the items in the order they are added to the list
- Important Methods:
- AddItem – This method adds strItem to the list box.
- Syntax: object.AddItem(strItem)
- Example lstNames.AddItem(“Patricia”)
- Clear – This method clears out the entire list, making it empty
again.
- Syntax: object.Clear()
- Example: lstNames.Clear()

Combo Boxes

- Combo boxes are a combination of a text box and a list box.


- Like a text box, it has an area where you can enter or display a single line of
data.
- Like a list box, it also can keep a list of data that can be viewed or selected.
- The three letter prefix for a combo box is cmb. For example a combo box of
cities might be named cmbCities.
- The combo box works in three modes:
- Drop-down combo – In this mode the user sees a text box where
they can enter values. On the right hand side of the text box is a button
with an arrow. If the user clicks the arrow, they will see a list of entries
made to the list box.
- Simple combo – In this mode, the user will see the text box, but
without the arrow. You must size the control to be larger in order to see
the list of available items (much like a list box). The user can type into the
text box area.
- Drop-down list box – This mode is much like the drop-down
combo except that the user cannot type into the text area. They can only
expand the list and select items from the list
- The combo box users the same properties as a list box (i.e. ListCount,
ListIndex. Text, Sorted).
- It also uses the same methods as a list box (i.e. AddItem, Clear)

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

- Option buttons enable the programmer to create a group of mutually exclusive


buttons. For example, in a word processor dialog box for formatting a
paragraph, there may be a set of option buttons that allow the user to select
left justification, right justification or centered text. The user can only select
one option.
- The three letter prefix for option buttons is opt.
- If the user selects one of the option buttons in the group then the other buttons
will be deselected.
- The option buttons are grouped by being placed inside of a frame object (see
frame object below).
- Like a check box, the option button has a Caption property and a Value
property.
- The Value property follows the same rules as a check box, however, only one
option button in a group can have its Value property set to True.
Frame

- The frame object is used to group option buttons.


- It can also be used simply as a visual aid to group related objects on a form to
make the form easier for a user to understand.
- It is not necessary to change the name property for a frame. If you do change
it then the three letter prefix should be fra.
- The frame has a Caption property which sets what is displayed at the top of
the frame.
- To group objects such as option buttons, create the frame, and change its size
to an adequate size for the object you plan to group. Then create the objects
you want as part of the group into the framed area. NOTE: The frame must be
created before you create the objects you want to place in them.
Lab 8: Text Formatting Dialog

Part 1
In this lab we will create a program which will allow us to format text inside a text box.

Create the Project

1. Start Visual Basic, and create a new project.


2. Save the form and project as TextFormat1

Create the Controls

Create these controls and place them according to the picture below. Note that the top
most control is a text box.

1. The names of your objects should be as follows: txtDoc, lstFont, cmbFontSize,


chkBold, chkItalic, chkUnderline, optBlack, optBlue, optRed, cmdApply and
cmdExit.
2. The tab order should be: txtDoc, lstFont, cmbFontSize, chkBold, chkItalic,
chkUnderline, optBlack, optBlue, optRed, cmdApply and cmdExit.
3. Make sure that the following objects behave properly
a. For the text box, it should have vertical scroll bars (ScrollBars property) and it
should be multi-line enabled (MultiLine property).
b. For the list box, make sure it is sorted.
c. For the combo box, it should be unsorted and the style should be Drop-Down
List Box

Setup Your General Declarations Area

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.

Write the Form Load Event

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.

1. Create the form load event


2. Add the following fonts to the font list box: Arial, Times New Roman, MS
Sans Serif, Courier, System, Symbol, and Papyrus. Note that the spelling is
important.
3. Make sure that the list is sorted.
4. Add the font sizes to the font size combo box. The valid font sizes should start
with 4 and end at 32 and only show even numbers (i.e. 4, 6, 8, 10….32)
5. Set the Text property of lstFont to the Font property of txtDoc (this ensures
that the selected item in the list box corresponds to the default font of the text
box.
6. Set the default font size to 8. Remember to set this default value for the text
box as well. You should use the FontSize property of the text box to set the
font size.
7. Set the Value property for the option button “Black” to True

Create the Apply Click 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 the Exit Click Event

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

Remove the Apply Button

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.

Add Click Events for all the Formatting Objects

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:

Let intNumberOfFonts = Screen.FontCount

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:

Let strFont = Screen.Fonts(intCurrentFont)

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.

You might also like