JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Sorting a Combo Box

You've been newly commissioned to write the guidebook to the 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, and it would be great if you could make that list appear in alphabetical order-but the zoo 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, ComboBox1. Now it doesn't matter in what order you add items to that combo box:

Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

The sorted combo box appears in Figure 7.14-now you'll be able to handle the animals from aardvark to zebra automatically and alphabetically.

Figure 7.14: Sorting the items in a combo box.

You should know, however, that sorting a combo box can change the indices 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.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor