|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Immediate SolutionsAdding Items To A List BoxThe Testing Department is calling again, and theyre telling you to get rid of all the beautiful buttons that youve placed on the main form of your program. But, you say, its a program that lets the user buy computer parts. We have to list what computer parts are available. Thats just it, they say, a list should go in a list box. So youve added your list box, and now its staring at you: a blank white box. How do you add items to the list box? You can add items to a list box at either design time or at runtime. At design time, you can use the List property, which is a very handy array of the items in the list box; and at runtime, you can use both the List property and the AddItem() method. Heres how you use the List property in code (keep in mind that you can get or set items in the list box with the List array): ListBox.List(index) [= string] How do you keep track of the total number of items in a list box? You use the ListCount property; that is, if you loop over the List array, youll use ListCount as the maximum value to loop to. At design time, you can add items directly to your list box by typing them into the List property in the Properties window. Selecting the List property displays a drop-down list (which is appropriate considering youre filling a list box), and you can type item after item into the list box that way. At runtime, you can either use the indexed List property as detailed previously, or the AddItem() method this way:
Private Sub Form_Load()
List1.AddItem (Item 1)
List1.AddItem (Item 2)
List1.AddItem (Item 3)
List1.AddItem (Item 4)
End Sub
Running this code gives us the list box in Figure 8.2.
We should note that when you place items in a list box, they are stored by index, and you can refer to them by their index with the List property. See the next topic for more details. Referring To Items In A List Box By IndexWhen you add items to a list box, each item is given an index, and you can refer to the item in the list box using this index (for example, you can get the items text by using the List property: List(index)). The first item added to a list box gets the index 0, the next index 1, and so on. When the user selects an item in a list box, you can get the selected items index with the list boxs ListIndex property. Lets see an example to make this clear. Here, we might just add four items, Item 0 to Item 3, to a list box this way with AddItem():
Private Sub Form_Load()
List1.AddItem ("Item 0")
List1.AddItem ("Item 1")
List1.AddItem ("Item 2")
List1.AddItem ("Item 3")
End Sub
This code places the four items into the list box with indexes 0 through 3 like this: List(0) = "Item 0" List(1) = "Item 1" List(2) = "Item 2" List(3) = "Item 3" Now we can refer to the items in the list box by index using the List property as List(0), List(1), and so on. When the user clicks the list, causing a Click event, we can display the item number the user clicked with the ListIndex property, which holds the index of the currently selected item:
Private Sub List1_Click()
MsgBox "You clicked item " & Str(List1.ListIndex)
End Sub
You can also change an items index with its Index property like this: List(index).Index = 3 In addition, you can sort items in a list boxsee Sorting A List Box later in this chapter. Responding To List Box EventsNow youve created your new list box, and its a beauty. The boss is very pleased with it when you show your new program at the companys expo. The boss clicks the list box with the mouseand nothing happens. The boss asks, Didnt you connect that list box to code? Oh, you think. Click And DblClick You use two main events with list boxes: Click and DblClick. How you actually use them is up to you, because different programs have different needs. For example, if a list box sets a new font that doesnt become active until a font chooser dialog box is closed, its fine to respond to the Click event to display a sample of the font the user has selected in a text box. On the other hand, if you display the names of programs to launch in a text box, you should probably launch a program only after a user double-clicks it in the list box to avoid mistakes. You use the Click event just as you use the Click event in a button, with a Click event handler. Here, we display the item in the list box the user has clicked, using the ListIndex property (you can get the selected items text with List1.List(ListIndex) or with List1.Text):
Private Sub List1_Click()
MsgBox "You clicked item " & Str(List1.ListIndex)
End Sub
And displaying the selected item is the same for DblClickyou just add a DblClick handler with the code you want:
Private Sub List1_DblClick()
MsgBox "You clicked item " & Str(List1.ListIndex)
End Sub
Note, by the way, that a DblClick event also triggers the Click event, because to double-click an item, you must first click it. Multiselect List Boxes List boxes can also be multiselect list boxes (see Using Multiselect List Boxes later in this chapter), which means the user can select a number of items in the list box. If your list box is a multiselect box, you can determine which items the user has selected by using the Selected property this way:
For intLoopIndex = 0 To List1.ListCount - 1
If List1.Selected(intLoopIndex) Then
...
End If
Next intLoopIndex
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |