listbox sort

Hello, I have a listbox with


COLUMNE is hidden (size 0).

Is there a way to sort COLUMNA (for example) by clicking to COLUMNA Header, but sorting for COLUMNE ?

Thank you for the support

To do that, you have to add your own header using a canvas (remove the ListBox Header) and deal with all features you want with your ListBox: sort for example.

So in Canvas MouseDown, you have to check where the user clicked (X, Y),
determine which Column is there (in whitch column the user clicked),
sort the corresponding Column (or the one you want).

The docs is your friend ListBox.Sort.

Is there a reason why you’re hiding the columne? If you just store your row-id in there it might be better to use the


For sorting purposes, you can use the ListBox.CompareRows event handler to sort column data however you want. It is often used to sort things in a non-text manner (such as number or dates) instead of the normal text sort you would get when the header is simply clicked.

Examples/Desktop/Controls/ListBoxExample demonstrates many of the feature of the ListBox, including a column with a custom sort.

Heres a bit of code I used to use that does just that. It goes in the HeaderPressed event handler.

[code] // If the user tries to sort by column 1, intercept it and sort by column 5 (use your own column numbers
Dim i As Integer

// for any other column, sort normally
if column <> 1 then
me.SortedColumn = column
return true
end if

// We can assume that the user clicked header 1
// Get the sort Direction of the column the used selected
i = me.ColumnSortDirection (column)
if i = 0 then
// set the opposite sort direction
i = 1
end if

if column = 1 then
// set the column to sort on and the sort direction, then sort it
me.SortedColumn = 5
me.ColumnSortDirection (5) = i
// set the original column’s sortdirection to match.
me.ColumnSortDirection(column) = i
end if
return true[/code]

Thank you all for your invaluable advice and in particular to Dale, was just what I was looking for. :slight_smile: