Unselect a Selected Row in Listbox

Xojo 2021 v3.1 / Mac

I have a Listbox. Row Selection Type is Single.
I select a row by clicking on it. It becomes highlighted.

Now how do I unselect that row? If there are visible blank rows at the end of the Listbox, I can click in that area and achieve the goal. But if there are hundreds of Listbox rows, then it is likely that the blank area at the end of the Listbox will not be visible, and I might have to scroll a long way to get there.

If I click on another row, the original row is unselected but the new row becomes selected. I want to return the state of the Listbox to nothing selected.

How does the user unselect a selected row?

at windows it is ctrl and click.
maybe similar at mac.
xojo listbox have also an option that select is required or not.

You’d have to add a keyboard shortcut, menu item, or context menu that calls a method to deselect all rows in a listbox.

The code for deselecting rows is:

Var n As Integer = Me.LastRowIndex

For i As Integer = 0 To n
  Me.RowSelectedAt(i) = False
Next i
1 Like

Click on it again, or on another row. Your logic in the Paint event can sort out what to do.

1 Like

Yepp. Cmd + Click

Something like a simple
Me.RowSelected(row) = Not Me.RowSelected(row)
may do the trick.

For me, Cmd-Click does not work on Mac.

In my situation, I have left that false which is what allows me to deselect the selected row if I click in the unused area at the bottom.

If I click on another row, then the behavior is already what I would want. The new row gets selected and the old row gets unselected.

If I click on the same row, I can capture the row in one of the Paint Events, but when I try and take advantage of this, the effect is simply to make it impossible to select any row. I suspect that the Paint event undoes the selection action immediately after it occurs. Here is the code I was trying in the PaintCellText event.

Var theSelectedRow As Integer
theSelectedRow = Me.SelectedRowIndex

If theSelectedRow = row Then
  Me.RowSelectedAt(theSelectedRow) = False
End If

I tried this approach and it worked. I put the following in the Pressed event of a button I called Unselect. The Listbox in question is named lbActiveApplescripts

If Self.lbActiveApplescripts.SelectedRowCount = 1 Then
  Self.lbActiveApplescripts.RowSelectedAt(Self.lbActiveApplescripts.SelectedRowIndex) = False
End If

While it works, I would prefer not to clutter my UI with this extra button (or menu item if that approach is used). But I can not figure out how to unselect the row by simply clicking on a selected row.

Works here with any native macOS List

Native

In the CellPressed event, add this, and a second click will deselect that row without buttons, keyboard shortcuts or menu items.

If Me.RowSelectedAt(row) Then
  Me.RowSelectedAt(row) = False
  Return True
End If
2 Likes

Command click works in the finder, but it doesn’t work in Xojo desktop Listboxes in several of my apps.

I just checked in a random Xojo project and Command-click on a selected row works as intended (deselects the clicked row).
Tested on a “simple” listbox, with multiple selection allowed.

Not here using Xojo 2021 R3.1 with a DesktopListBox on Monterey.

Maybe, because they are not native. But you already provided a solution to this. :slight_smile:

I am sure you can make it “feel” like a bit more native by doing something like:
If Keyboard.AsynckeyDown(...) And Me.RowSelectedAt(row) Then...

1 Like

This seems to be the key. It works if multiple selection is allowed but does not if single selection is specified.

I tested both with the old ListBox and DesktopListbox

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.