Setting setting values in an array of booleans

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:

ColumnSortAsc()

But I keep getting an error when I attempt this:

ColumnSortAsc(Column)

or explicitly like this

ColumnSortAsc(0)

Obviously I’m missing somethings very basic here.

What error ?

OutOfBoundsException.

You need to initialize the array to the appropriate size. See REDIM in the manual.

If you are defining it like this

ColumnSortAsc()

and accessing it like this

ColumnSortAsc(0)

I’ll bet your error is “OUT OF BOUNDS”
Reason? you didn’t provide a SIZE to the array, you created a dynamic array, with no elements

yeah… what Tim said :slight_smile:

Why not use the built-in ColumnSortDirection property of the Listbox?

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.

To modify the standard sort check out the CompareRows event in Listbox

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.

what do you need to do in the listbox enabled the shift key to sort multiple column??

This is a very simplified version of what I do:

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