Listbox: Set background color when item selected by program

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
I have a listbox called lbxDBType. When a value is clicked it is selected and the background color is set blue to highlight it.

If my program selects a value by executing
lbxDBType.SetSelected(IX, True)​
this doesn't happen: all values remain plain.

Is there a property that I can set that makes it behave the same way as a click? Or a statement that I should add?

Thank you, Robert.
 
Firstly, what technology are you using? As you mention a SetSelected method, I'm assuming that it's Windows Forms. In that case, this thread should have been created in the Windows Forms forum. Please don't be lazy and post everything in the VB.NET General Discussion forum. Post in the most appropriate forum. That way, we don't have to waste our time and yours with questions like what technology are you using. Thread moved.
 
As for the question, I'm guessing that it is answered by the documentation:
You can use this property to set the selection of items in a multiple-selection ListBox. To select an item in a single-selection ListBox, use the SelectedIndex property.
So, what is theSelectionMode of your ListBox set to? If it's One then that's your answer and this demonstrates why you should ALWAYS read the documentation. It takes seconds to find it via the Help menu so there's really no reason not to.

If the SelectionMode is set to MultiExtended or MultiSimple then there's something else at play but I'd need confirmation of that to look into it further.
 
I had in fact read this help page, but by the time I'd got to the end of the example (which used SetSelected, so of course this is what I used in my code) I read " items in a multiple-selection ListBox" as simply a list of items in a listbox from which you selected one, the help didn't mention SelectionMode and I didn't make the connection. So please don't assume that I'm too lazy to read documentation, nor that the MS documentation is always crystal clear. I had in fact spent a couple of hours reading this page and others looking for the solution before I posted a question on the forum.

But thanks for solving the problem for me.
 
I read " items in a multiple-selection ListBox" as simply a list of items in a listbox from which you selected one
I'm not sure why you would think that given that that would mean that a single-selection ListBox would be one with a single item in it. Why would that be a thing? There's no logic to that at all. Also, the ListBox documentation says this:
A ListBox control can provide single or multiple selections using the SelectionMode property.
It's good that you do/did read the documentation but you did a rather poor job of processing what you read on this occasion.
 
Back
Top