|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Sorting A Combo BoxYouve been newly commissioned to write the guidebook to the local zoo with Visual Basic, and everything looks greatexcept 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, its 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 doesnt 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 youll be able to handle the animals from aardvark to zebra automatically.
Clearing A Combo BoxIts time to put new items into a combo boxbut 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. Heres 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, theyre gone until you expressly add them again. Locking A Combo BoxYou can lock a combo box by setting its Locked property to True. When locked, the user cannot enter text in the combos text box and cannot make selections from the combos list (although if the list is drop-down, it will still open). However, when programmers think of locking a combo box, its not usually the Locked property that they want. The more common operation is to restrict the users ability to enter text in a combo box so that he must instead select one of the items in the combos list. You can make sure that the user cant enter text in the combo boxs text box by setting the combo boxs Style property to VbComboDrop-DownList. Here are the settings for the combo box Style property:
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).
|
|
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. |