HOME   DOWNLOADS   ORDER ONLINE   CONTACT US



 OVERVIEW
 
 DOWNLOAD
 
 ORDER ONLINE
 
 KNOWLEDGE
 BASE

How to use the Sorted property

The Sorted property of an SBList can be changed at runtime, something which a standard list box does not offer. This makes displaying list items exactly how you want them much easier.

For example, suppose you wanted to show a list of users in your program, sorted into alphabetical order, but have a special user called "Visitor" which allowed access into the program but with reduced privileges. It would be much clearer if this Visitor name appeared at the top of the list, with the names listed alphabetically below, but how would you normally program that? The easiest way would be to have a sorted list box hidden away on the form somewhere; dump the users into it so that they get sorted, then copy them into your visible list and add the "Visitor" name at the top? Sound familiar? Here's how to do it with just one SBList:

For this demonstration you will need to draw a form which has a single SBList on it.

Insert the following code into your Form_Load event:

Names$ = "Fred,Steve,Andy,Cindy,Clive,Sandra,George,"
SBList1.Sorted = True
Do While Names$ <> ""
  P& = Instr(Names$, ",")
  SBList1.AddItem Left$(Names$, P& - 1)
  Names$ = Mid$(Names$, P& + 1)
Loop
SBList1.Sorted = False
' Add this item at the top of the list...
SBList1.AddItem "Visitor", 0
As you can see, once the Sorted property is turned off (at the end of the code) you can add any items to the list exactly where you want them.

If you turn the Sorted property on and off, adding items here and there, the final sort order will probably not be what you expected. Once you have turned off sorting and added more items where you wanted them to go, turning Sorted back on and adding more items may not place them in the order you expected. In fairness, you would be missing the objective behind a controllable Sorted property:

Normally, when you draw a list box on a form you have to choose whether it will be sorted or not, and then you're stuck with it. The Sorted property of the SBList is changeable but the intention was that you would only alter the setting when the list is empty. In other words, you would decide at runtime whether you wanted to display items sorted or in their original order, and set the property accordingly before populating the list.

The sample code above shows you how to take advantage of this feature to give you control over the order of list items, and you may discover even further refinements, but be warned that changing the Sorted property too often may give you sleepless nights!

Topic devised by Andy Groom.

 
Copyright © 2000 Global Components. All rights reserved.