Why won't my listbox sort?

I have a desktoplistbox withe a header row and a couple of columns. I use the paintheaderbackground and paintheadercontent events to make a nice-looking header, but I can’t get sorting to work. In the HeaderPressed event (which does fire) I have this:

me.SortingColumn = column
me.ColumnSortDirectionAt(column) = DesktopListBox.SortDirections.Ascending
me.Sort ()
Return False

but the content is not sorted. I even added the RowComparison event (all it does is return false), and that fires too.

I tried adding:

me.HasHeader = True

to the above code. all that did was give me a stack overflow exception.

How can I get sorting to work? If I use vanilla headers on a listbox, and essentially no event handlers, then it sorts as expected.

With apologies for the basic questions:

What is the value of column?
How many columns does your listbox have?
Do you draw the values in the cells in code, or are they ‘just text’ ?

In a new project, I add a Listbox to a window, with two columns

This code in the Opening event, works fine:

for x as integer = 0 to 30
  dim y as integer = Rnd() * 30
  Listbox1.AddRow y.ToString("00")

listbox1.SortingColumn = 0
listbox1.ColumnSortDirectionAt(0) = DesktopListBox.SortDirections.Ascending


Text in both columns. There’s a third column, of width zero, whose only purpose is ti make the little square in the header whch is above the top of the scroll bar be coloured like the rest of the header.

In non-event code, I add Celltags for each cell. Column 0 celltag is an id, there so I can, later, use it to remove a database row as the listbox row is deleted. Column 1 celltag is a text value.

Then in the paint event I also use the id to look up the text to put in col 0, and for col 1 just copy the celltag to the celltext. I could, arguably simplify some of that a bit but at this point I want to get the sort going first. One assumes that there’s nothing to prevent one putting the sort request code in the HeaderPressed event, otherwise what purpose would that event serve. The docs are skimpy on the subject.

That sounds like the problem to me.
Isn’t there any text in there before the paint event occurs?
Do you just .drawstring the text during the paint event instead of setting the celltextat() ?

if the data comes from a database, wouldn’t you sort the data in your query?

‘putting the sort request code in the HeaderPressed event, otherwise what purpose would that event serve.’

A listbox will sort itself if you click the (assumed present) header and the column has text in it.

There won’t be any text there before the first paint event for any row. I also established that if you do nothing for a cell (no text or tag in it) then the paint event doesn’t fire for that cell.

So yes, in the paint event I use DrawText to create the cell text, having either looked in a database for it (col 0) or looked at the celltag (col 1).

As for sorting the data as it comes from the database, I actually do that for another listbox. I also draw the little up/down arrows in the header too, in that instance. Here, the data is structured a bit differently, and I have to get the col 0 data one select at a time, but I think I should take another look at that and see if there’s some clever ORDER BY that I can use.

Ah, maybe it’s because there’s never any real text in any of the cells, only drawn text.

1 Like

So there isn’t any actual text in the cell, even though it looks like there is.
The sort is happening on a column that holds no words.
And if there is no text, Paint() doesnt fire either.

Use cellTextAt() to actually put text in the cell at the same time as you set the celltag.

You may be able to solve this by using the RowComparison event.

Yes, that’s probably why I hit on getting SQLite to do the sorting, in the other listbox. At least for that one it was easy. I just did that and moved on to the next problem.

Thanks - you forced me to think, much the best way to learn :thinking:

May not need to - but I’m being paged for supper and a DVD.