Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting a listbox

Status
Not open for further replies.

PrograMan

Programmer
Jul 21, 2001
59
US
I'm having the worst time trying to sort the items in a listbox of variable length with each item being variable lengths themselves. I thought I would just do a simple bubble sort with it, but every time I set it up, there's always something that the compiler doesn't like. I dunno if it's incorrect coding or my compiler itself. Whatever. Will someone please show me the correct way to do this? The list is called, unoriginally, "List1".
 
Did you try:
[tt]
List1.Sorted = True
[/tt]

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
That doesn't work during run time. The compiler will only accept that during design time. Consequently, when the items are put into the listbox by the user during run time, the program will not automatically sort them on the fly. That's why a bubble sort is called for.
 
Click on the listbox, in the properties window set the listbox sorted = true
It works all the time not as you suggested.
 
If you set sorted = True at design time, then new items added are normally added in their sorted order unless you add them with the Index set. If the Index is set then the new item goes in at the Index value

Use:
List1.AddItem Text1.Text


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
My compiler doesn't allow for automatic sorting like that. I don't know why. It did occur to me to use it, but I got an error about it. First, if I put in ".Sorted =", it says that I cannot assign to read-only property. If I take out the "= True", it says "Invalid use of property."

If there were a way (maybe some other type of additional code for it to be accepted) for me to use this property, I'm all ears (or eyes).
 
Er...the idea that johwnm and LPlates are suggesting is that, at design time, you modify the Sorted property for the listbox using the control's Properties window, not add code for the control (since the code, naturally enough, only runs at..uh...run time, and gives you the errors you are reporting).

Just in case you don't actually have the Properties window dialog showing in the IDE, try View/Properties Window (or press F4)
 
I see... however, it seems it doesn't matter whether I adjust it in the property window or try to stick a code in, my program just doesn't want to sort automatically like it sounds that they're suggesting. I've already tried all that stuff, so I've found myself having to resort to the bubble sort.
 
How are you adding items? As johnwm points out, if you use the Index parameter whilst adding the item will not get sorted...

' List1.Sorted must be set as True in IDE for this demo to work
List1.AddItem "A"
List1.AddItem "C"
List1.AddItem "D"
List1.AddItem "E"

List1.AddItem "F", 3 ' Here's where we use the index parameter, which prevents this item from being sorted

List1.AddItem "B"

' Remembering that listbox positions start at 0, at the end of this, Listbox will have A,B,C,D,E correcty sorted, but F will have been originally
' inserted at position 1 and then slipped into position 2 because of the sorted insertion of B
 
Oops:

List1.AddItem "F", 3

should have read

List1.AddItem "F", 1
 
The inserted items will not be completely alphabetical from the beginning. The order of insertion could be, say,

List1.AddItem "feather"
List1.AddItem "magazine"
List1.AddItem "bicycle"
List1.AddItem "glue"
List1.AddItem "lamb"
List1.AddItem "attack"
List1.AddItem "their"

...
etc.

As you can see, no index has been set anywhere, but List1.Sorted in the properties window does not want to sort this out at run time.
 
Y'know what? I just saw something in my program that I had overlooked and now understand. I've fixed the problem and it works way I want it now. Thanks everyone! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top