|
|
 |
 |
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Chapter 8 List Boxes And Combo Boxes
- If you need an immediate solution to:
- Adding Items To A List Box
- Referring To Items In A List Box By Index
- Responding To List Box Events
- Removing Items From A List Box
- Sorting A List Box
- Determining How Many Items Are In A List Box
- Determining If A List Box Item Is Selected
- Using Multiselect List Boxes
- Making List Boxes Scroll Horizontally
- Using Checkmarks In A List Box
- Clearing A List Box
- Creating Simple Combo Boxes, Drop-Down Combo Boxes, And Drop-Down List Combo Boxes
- Adding Items To A Combo Box
- Responding To Combo Box Selections
- Removing Items From A Combo Box
- Getting The Current Selection In A Combo Box
- Sorting A Combo Box
- Clearing A Combo Box
- Locking A Combo Box
- Getting The Number Of Items In A Combo Box
- Setting The Topmost Item In A List Box Or Combo Box
- Adding Numeric Data To Items In A List Box Or Combo Box
- Determining Where An Item Was Added In A Sorted List Box Or Combo Box
- Using Images In Combo Boxes
In Depth
In this chapter, were going to take a look at two popular Visual Basic controls: list boxes and combo boxes. These controls present the user with a list of items that the user can select from, and every Windows user is familiar with them.
List boxes do just what their name implies: display a list of items. The user can make a selection from that list, and Visual Basic will inform our program whats going on. Because list boxes can use scroll bars if a list gets too long, these controls are very useful to present long lists of items in a way that doesnt take up too much space.
Combo boxes are list boxes combined with text boxes. With combo boxes, you can give users the option of selecting from a list (usually a drop-down list activated when users click the downwards-pointing arrow at right in a combo box) or typing their selections directly into the text box part of the combo box.
List boxes and combo boxes share many properties, so it makes sense to look at them in the same chapter. The reason they share so many properties is that the basis of working with list boxes and combo boxes is item selection. For example, if your program lists various books for sale, you can present their titles in a list; clicking a books name can display more information about the selected book. If you want to let the user set font size in a program, you might present font sizes in a combo box, and when the user selects a font size, the program can then read the selected size from the combo box.
Both list boxes and combo boxes are controls intrinsic to Visual Basic (in other words, you dont have to add them). You add list boxes to a form with the List Box tool, which is fifth down on the right in the toolbox in Figure 8.1, and combo boxes with the Combo Box tool, which is the fifth tool down on the left in the toolbox. Theres nothing special about these controls hereyou just add them as usual with the tools in the toolbox.
Figure 8.1 The List Box tool and the Combo Box tool.
In overview, heres how you work with both list boxes and combo boxes: To add or delete items in one of these controls, use the AddItem or RemoveItem methods. You can use the List, ListCount, and ListIndex properties to enable a user to access items in the control (or you can add items to the list by using the List property at design time). When the user makes a selection, you can determine which item was selected with the ListIndex or Text properties.
Both list boxes and combo boxes have Click and DblClick events, and the way you use these events depends on how youve defined your user interface (in other words, does clicking an item to select it in one of these controls launch an action, or does the user have to double-click the item?). Its important to realize that a combo box really is a text box and a list boxthe Click events only work for the list part of a combo box. When the user makes changes to the text box part of the combo box by typing into that text box, a Change event (as is usual for text boxes) is triggered.
Thats how you use the list box events: Click when the user clicks the list box, and DblClick when the user double-clicks it. For combo boxes, its a little more complex: Click when the user clicks an item, DblClick when the user double-clicks an item (the Style of the combo box must be set to Simple, Style = 1, for the DblClick event to work), and Change when the user enters text. Note in particular that when the user makes a selection in a combo boxs list box that changes the text in the text box, a Change event is not triggered; the Change event only occurs when the user types text into the combo box.
Thats all the overview we needwell turn to the Immediate Solutions now.
|