I would like to enable column sorting by clicking the header. Tried following the documentation (eg listbox.sorttypes,etc) but my efforts are not recognised. How can I do this, please?
For a listbox it is automatically on. You need to check the had headers box.
Could you elaborate on “my efforts are not recognized” ?
Ian understood this to mean you don’t have headers visible. I’m understanding this to mean that the data isn’t sorted the way you want – which comes with different explanations and different solutions.
We need to know what’s actually happening to help you better.
Has headers is enabled and are visible, bolded and background colour is cyan. Clicking a header has no effect. Data is loaded from a csv file.
Are we on Desktop or Web? On Desktop clicking the column header adds a up or down pointing arrow to the right end of the header. It sorts the data in the column alphabetically. For web I don’t know as I’ve not used it since Web2.
Thanks for your support chaps. Silly me was clicking on the cell rather than the heading numbers above - that works as hoped for.
Well not quite as required - the header row gets lost!
Lets go back to square one.
My requirement is to be able to sort a llstbox by clicking on the header cell not the column numbers above and not losing the header row (row 0). i.e. sorting from row 1
Any ideas please?
The header row doesn’t have a row number. The first row (not the header) is row 0. If you want headers, you have to click the “Has header” property in the Inspector.
Then you can use the PaintHeaderContent event to put something nice in the header cells.
Your header row must be converted to the Header (replace the column numbers with the header row information).
Thanks Tim but still being a xojo novice I need some coding assistance to enable me to use HeaderPaintContent etc. HasHeader is enabled in the Inspector.
OK so what do you want in the header cells? And how do you want each column sorted? Are all columns (say) numeric or do you have some numeric and some with text?
Progress has been made! Assignments for each column achieved with me.headerat(x)=“Mickey Mouse” etc… How do I color the background to cyan and the font to Times New Roman Bold? A numeric sort would be nice as well.
I assume you mean each column header?
Again I assume this refers to header cells?
Read the doc for the DesktopListBox.PaintHeaderContent
event, and for Graphics
. You’ll want to set some of the properties of your Graphics and use methods like FillRectangle
and DrawText
.
If you read the info on sorting for a Listbox, you’ll see that for a numerical sort you need to implement the RowComparison
event handler.
Ian, I know numeric sort is possible, but your response doesnt tell me how. Where is the code and what does it look like.
By the way Tim your recommendation to read the docs doesnt yield enough depth of info, while the new online doc is absolutely useless. Try searching for headerpaintcontent.
As @TimStreater said. Look at the RowComparison event. The example code provides exactly what you are asking for:
Function RowComparison(row1 As Integer, row2 As Integer, column As Integer, ByRef result As Integer) As Boolean
Select Case column
Case 0 // This is a string column. Let the listbox manage it by returning false
Return False
Case 1 // This is our numerical value column. Let's do the work ourselves
If Me.CellTextAt(row1, column ).Val < Me.CellTextAt(row2, column).Val Then
result = -1
ElseIf Me.CellTextAt(row1, column).Val > Me.CellTextAt(row2, column).Val Then
result = 1
Else
result = 0
End If
Return True
Else //some other column for which we let the listbox handle comparison
Return False
End Select
End Function
Read the help, it has better options too:
Find the old docs at docs.xojo.com.
Search for DesktopListbox. Click on RowComparison or HeaderPaintContent. These as previously mentioned are events so you’ll find them in the Events section.