prevent row selection in Listbox

Is there any way to prevent a Listbox row being selected when clicked on?

I have created a class based on ListBox with a mode property.I want rows to be selected when clicked on in “edit” mode but not when in “read” mode.

I’ve tried various things in the cellclick and backgroundpaint events but I can’t find a way to prevent selection in “read” or to cancel it immediately after it has happened. Is there a quick fix?

In the Listbox.Change event put

me.ListIndex = -1

The selection will still flash, but can’t be selected

If you want to prevent any row from being selected you can return true in MouseDown to prevent the row flash.

Thanks guys, those work fine. Unfortunately, they create another couple of problems for me.

At the moment, in read mode, if the user double clicks a cell, the Listbox puts the cell contents onto the clipboard for pasting into another document. That will no longer work.

I also need to be able to respond to a click to access the celltag and the text in any cell where the celltag is “”. That’s because the cell will contain a URL and I want to call a showURL to open the dafult browser and go there.

I just don’t want to actually select the row as if I intended to edit it, but maybe I have to put up with that in order to do these things?

You might be able to fake a non-selection with CellBackgroundPaint and CellTextPaint by setting them to the unselected colors if you’re in read mode. Not the greatest solution but it could work for you.

Thanks. I did try that, but I couldn’t get it to work and anyway I think I’m probably being a bit too finicky seeing as it’s part of the functionality of the Listbox to be able to select things. Thanks for the suggestions.

Quick example project that shows it working: Lying Listbox.xojo_binary_project
(yes, I like stupid filenames)

Hey thanks, Tim. That’s a really good compromise. Thanks for your help!