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


Getting The Number Of Items In A Combo Box

You’re trying to bend over backwards to make your program user-friendly and have let the user add items to the main combo box. But now you need to see if he has added a particular item to the combo box. How do you find out how many items there are in the combo box currently so you can set up your loop?

You can use a combo box’s ListCount property to determine how many items are in the combo box’s list. Let’s see how to use ListCount in an example. Here, we’ll search the items in a combo box for one particular item with the caption “Item 1”, and if we find it, we’ll display a message box.

We start by setting up our loop over the indexes of all the items in the combo box this way (note that we subtract 1 from ListCount because indices are zero-based):

Private Sub Command1_Click()
    Dim intLoopIndex As Integer
    For intLoopIndex = 0 To Combo1.ListCount - 1
...
    Next intLoopIndex
End Sub

Then we search the indexed List property for the item we want and, if we find it, report that fact to the user:

Private Sub Command1_Click()
    Dim intLoopIndex As Integer
    For intLoopIndex = 0 To Combo1.ListCount - 1
        If Combo1.List(intLoopIndex) = "Item 1" Then
            MsgBox "Found item 1!"
        End If
    Next intLoopIndex
End Sub

Setting The Topmost Item In A List Box Or Combo Box

One of the properties of list and combo boxes, the TopIndex property, has fooled a lot of programmers, because according to Microsoft, this property lets you set the topmost item in a list box or combo box’s list. However, what that seems to mean is not exactly how it works.

When you set a list box or combo box’s TopIndex property to some value, the items in the list are not reordered (if you want to reorder them, use the items’ Index properties or the control’s Sorted property). What TopIndex does is to set the topmost visible item in the list in those cases where not all items in the list are visible (in other words, if the list has scroll bars on the side).

Let’s see an example. Here we place some items into a simple combo box (in other words, a simple combo box has its list permanently open and its Style property is set to VbComboSimple, whose value is 1):

Private Sub Form_Load()
    Combo1.AddItem ("Item 0")
    Combo1.AddItem ("Item 1")
    Combo1.AddItem ("Item 2")
    Combo1.AddItem ("Item 3")
End Sub

When the user clicks a command button, we can make Item 2 topmost in the visible portion of the list:

Private Sub Command1_Click()
    Combo1.TopIndex = 2
End Sub

The result appears in Figure 8.10. When you click the button, the list scrolls automatically so Item 2 is the topmost visible item (note that this scrolling operation only occurs if not all items in the list are visible at once).


Figure 8.10  Making an item topmost.


TIP:  The reason for TopIndex’s existence is to make life easier for users when they are working with long lists. Each time they reopen a list, it’s a pain to have to scroll down to the former location just to be able to select the following item. For this reason, programs often “remember” the last-selected item in a list and make that topmost when the list is opened again.

Adding Numeric Data To Items In A List Box Or Combo Box

You’ve been asked to write the employee phone directory program and place a combo box with all the employee’s names in the middle of a form. Now how do you connect phone numbers to the names?

You can use a list box’s or combo box’s ItemData array to hold Long integers, because that array is indexed in the same way as the control’s items themselves are indexed. That is, you can store numeric data for Item 5 in the list or combo box in ItemData(5).

Let’s see an example to make this easier. Here, we add four items to a combo box when its form loads:

Private Sub Form_Load()
    Combo1.AddItem ("Item 0")
    Combo1.AddItem ("Item 1")
    Combo1.AddItem ("Item 2")
    Combo1.AddItem ("Item 3")
...

Next, we add numeric data to each item in the combo box:

Private Sub Form_Load()
    Combo1.AddItem ("Item 0")
    Combo1.AddItem ("Item 1")
    Combo1.AddItem ("Item 2")
    Combo1.AddItem ("Item 3")

    Combo1.ItemData(0) = 0
    Combo1.ItemData(1) = 111
    Combo1.ItemData(2) = 222
    Combo1.ItemData(3) = 333
End Sub

Now when the user clicks an item in the combo box, we can indicate what that item’s numeric data is with a message box:

Private Sub Combo1_Click()
    MsgBox "Data for the clicked item is: " & _
        Str(Combo1.ItemData(Combo1.ListIndex))
End Sub

In this way, you’re able to store more than just text for list or combo box items.


TIP:  Associating simple numbers with your list or combo box items isn’t enough? What if you have more data? Try using the ItemData value as an index into an array of data structures instead.

Determining Where An Item Was Added In A Sorted List Box Or Combo Box

You’re letting the user customize a combo box by adding items to the combo box, and in code, you place data into the ItemData array for this item after it’s been added. But there’s a problem—this is a sorted combo box (or list box), which means you don’t know the item’s actual index when it’s added, and you therefore don’t know its index in the ItemData array. How can you find out where the item was placed in the sorted combo box?

You can use the control’s NewIndex property to determine the index of the most recently added item to the control. For example, let’s say that the user can add items to a sorted combo box by placing the text of the new item in a text box and clicking a command button:

Private Sub Command1_Click()
    Combo1.AddItem (Text1.Text)
End Sub

The index of the new item in the sorted list is now in the NewIndex property, so we can add data to the new item’s entry in the ItemData array (if you don’t know what this array does, see the previous topic) and display that data in a message box this way:

Private Sub Command2_Click()
    Combo1.ItemData(Combo1.NewIndex) = 10000
    MsgBox "Data for the new item is: " & _
        Str(Combo1.ItemData(Combo1.NewIndex))
End Sub


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.