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


Sorting A Combo Box

You’ve been newly commissioned to write the guidebook to the local zoo with Visual Basic, and everything looks great—except for one thing. The program features a combo box with a list of animals that the user can select to learn more about each animal, and it would be great if you could make that list appear in alphabetical order. The zoo, however, keeps adding and trading animals all the time. Still, it’s no problem, because you can leave the work up to the combo box itself if you set its Sorted property to True (the default is False).

For example, say we set the Sorted property to True for a combo box, Combo1. Now it doesn’t matter in what order you add items to that combo box

Private Sub Form_Load()
    Combo1.AddItem ("zebra")
    Combo1.AddItem ("tiger")
    Combo1.AddItem ("hamster")
    Combo1.AddItem ("aardvark")
End Sub

because all the items will be sorted automatically. The sorted combo box appears in Figure 8.9. Now you’ll be able to handle the animals from aardvark to zebra automatically.


Figure 8.9  Sorting the items in a combo box.


TIP:  You should know, however, that sorting a combo box can change the indexes of the items in that combo box (unless they were already in alphabetical order). After the sorting is finished, the first item in the newly sorted combo list has Index 0, the next Index 1, and so on. If you want to change the indexes of the items back to their original values, you can set their Index properties.

Clearing A Combo Box

It’s time to put new items into a combo box—but does that mean you have to delete all the current items there one by one with RemoveItem()?

No, you can clear a whole combo box at once with the Clear() method. Here’s an example. First, we add items to a combo box:

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

Then we can clear the combo box when the user clicks a command button:

Private Sub Command1_Click()
    Combo1.Clear
End Sub

Note that there is no “unclear” method! Once you remove the items from a combo box, they’re gone until you expressly add them again.

Locking A Combo Box

You can lock a combo box by setting its Locked property to True. When locked, the user cannot enter text in the combo’s text box and cannot make selections from the combo’s list (although if the list is drop-down, it will still open). However, when programmers think of “locking” a combo box, it’s not usually the Locked property that they want.

The more common operation is to restrict the user’s ability to enter text in a combo box so that he must instead select one of the items in the combo’s list. You can make sure that the user can’t enter text in the combo box’s text box by setting the combo box’s Style property to VbComboDrop-DownList. Here are the settings for the combo box Style property:

  VbComboDropDown—0; drop-down combo box. Includes a drop-down list and a text box. The user can select from the list or type in the text box. (This is the default.)
  VbComboSimple—1; simple combo box. Includes a text box and a list, which doesn’t drop down. The user can select from the list or type in the text box. The size of a simple combo box includes both the edit and list portions. By default, a simple combo box is sized so that none of the list is displayed; size the combo box to display more of the list.
  VbComboDrop-DownList—2; drop-down list. This style allows a selection only from the drop-down list. This is a good one to keep in mind when you want to restrict the user’s input, but if you want to use this one, you should also consider simple list boxes.

Besides locking or setting the Style property of a combo box, you can also disable a combo box, of course, by setting its Enabled property to False; however, this grays out the control and makes it completely inaccessible. Another option is to make the combo box disappear by setting its Visible property to False (setting the Visible property to True makes the combo box reappear).


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.