I’m having a problem setting the value of an element in an array of booleans. I want to have a Property that stores the sort direction of a ListBox Column. I see I can have one such boolean Property per column, like this:
Column0SortAsc
Column1SortAsc
etc.
Using such Properties, I can successfully set any one I want like this:
Column0SortAsc = True
Column1SortAsc = False
etc.
But I want to decrease the number of such Properties to a single array of booleans:
Ah. Okay. I see that now about arrays. REDIM fixes this.
The reason I am not using the ColumnSortDirection property of the Listbox is that I want more control. I want to be able to sort based on more than one column, and I want to honor the number of rows desired each time the sort is triggered. So, for the latter, I need to re-access the database file with each sort, allowing the SQL LIMIT condition to be honored.
As far as I can tell, the built-in sorting of Listbox is a single column event. Correct me if this isn’t the case. I’m still very new at Xojo.
Thanks, Jim. I was aware of CompareRows, but since I want to maintain a particular number of rows in the ListBox (which can be set by the user), I have to reload the data from the database with each Sort call. So I figured I would just rely on SQL ORDER BY condition to do the sorting for me.
I also have implemented a multi-column sort. The user can click on a header cell, which sorts by this column immediately, or he can hold Shift down, and then click on several header cells to do a multi-column sort. It sorts when the Shift key is released.
As ColumnSortDirection is a property applied to each column separately, you can use them. For example changing ColumnSortDirection of column 3, does not change the ColumnSortDirection of the other columns. ColumnSortDirection is not sorting, it only holds the information of the last time it was clicked or changed by code.
Event HeaderPressed(column As Integer) As Boolean
If Keyboard.AsyncShiftKey Then
mColumns.Append(column) // Collect the columns to sort by
Return True // Prevent automatic sorting
Else
If mColumns.Ubound > -1 Then // This is a multi-column sort
mColumns.Append(column) // Collect the last column to sort by
... // Do the multi-column sort here
Me.Invalidate()
ReDim mColumns(-1) // Empty the column array for the next sort
Return True // Prevent automatic sorting
Else // This is a single-column sort
Return False // Let Xojo sort autiomatically
End
End
End
The user presses “Shift” and holds it, then he clicks on the the column headers and before he clicks on the last column he has first to release the “Shift” key. So the order is:
Shift Click 1 … Click n-1 Release Shift Click n
The alternative is to have a timer, in which you check for the Keyboard.AsyncShiftKey not being hold down anymore to start the multi-column sort (the Shift key is not captured by KeyDown and KeyUp). Then the user will be able to do the following:
Shift Click 1 … Click n Release Shift