Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


Immediate Solutions

Adding Items To A List Box

The Testing Department is calling again, and they’re telling you to get rid of all the beautiful buttons that you’ve placed on the main form of your program. But, you say, it’s a program that lets the user buy computer parts. We have to list what computer parts are available. That’s just it, they say, a list should go in a list box.

So you’ve added your list box, and now it’s 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. Here’s 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, you’ll 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 you’re 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.


Figure 8.2  Placing items in a list box.

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 Index

When 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 item’s 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 item’s index with the list box’s ListIndex property. Let’s 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 item’s index with its Index property like this:

List(index).Index = 3

In addition, you can sort items in a list box—see “Sorting A List Box” later in this chapter.

Responding To List Box Events

Now you’ve created your new list box, and it’s a beauty. The boss is very pleased with it when you show your new program at the company’s expo. The boss clicks the list box with the mouse—and nothing happens. The boss asks, Didn’t 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 doesn’t become active until a font chooser dialog box is closed, it’s 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 item’s 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 DblClick—you 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


Previous Table of Contents Next


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.