Is there a simple way to create a Listbox that the user cannot modify. I would like a Listbox that I could populate with code, but that the user cannot change. Jade originally told me:
Yes, you can prevent row selection by using the SelectionType property, setting it to ListBox.SelectionTypes.None. You can set this in the Inspector or in code, e.g. in the Opening event of the ListBox or the Window.
However, when I pointed out that I saw only Single or Multiple as options for SelectionType in the IDE, Jade retreated and said that it had been wrong. It claimed that the best workaround was to deselect the row immediately in the SelectionChanged event.
Is this true? On a Mac, using 2026 R 1.1, this works, but there is a flash of background selection color when the user tries to click on a row, which I find suboptimal.
SelectionChanged occurs after the row has been chosen which is why you get the flash.
MouseDown occurs before the row selection is updated so you can use that to ignore the click. However, this would also catch clicks in the header / scrollbar so you would have to take those into account.
The ListBox selection can also be modified via the cursor keys so you would also have to handle that.
CellClick event return true on ListBox
CellPressed on DesktopListbox
This is all you need to “disable” an enabled listbox, no mouse events get to the listbox so no selection changes occur. You cannot click or do anything.
Can you define “cannot modify”?
For the text you wrote next it seems that you want the user not be able to select a row, if that is the only thing missing you can add PaintCellBackground and just put Return True there.
Is there something else you need to do? I guess you don’t want to disable the Listbox.
I have a canvas in front of the listbox which gives the listbox a bit of a greyish appearance when the listbox is not editable. The canvas also handles the mousedown:
Looks like that assumes Light mode and may look wrong for Dark Mode.
As said in my previous comment, you only need to ‘Return True’ in PaintCellBackground:
Function PaintCellBackground(g As Graphics, row As Integer, column As Integer) Handles PaintCellBackground as Boolean
Return True
End Function
Yeh, it probably would be wrong in Dark Mode but thats an exercise for the reader.
My tests seemed to indicate that just using ‘Return True’ in the paint background event resulted in the selected row text not appearing as Xojo had set the text colour to white.
What version of Xojo are you using?
I did my tests on a mac in Dark Mode with Xojo 2025r3.1 changing the setting from Dark Mode to Light mode (after each run) and it works correctly with just ‘Return True’ in the mentioned event.
But my immediate reading of this is that you don’t want the user to edit the text. Unless you proactively add DesktopListBox.EditCellAt(row, column)–which again would probably be in the CellPressed or SelectionChanged events, they shouldn’t be able to.
I get the same as kevin, and it’s been like that as long as I remember.
[now speaking about light mode as it’s the only one I use]
In Xojo’s listboxes, when there’s a selection, the text is normally white. If you return true from the CellBackgroundPaint event, it indeed stops drawing the background (selection), but the text itself continues to reflect the selected state. So both the text and background are white.
This meets my needs. Thanks. I click on the row, and there is no flash or anything. It is just dead. This is what I wanted. In the CellPressed event just