views:

529

answers:

2

Do I programmatically have to manage the backcolor\highlight color on a Listview's item when selected through code?

So if I do this: listView1.Items[1].Selected = true;

Do I also need to do this, so it looks highlight, as it does when selected with a mouse click: listView1.Items[1].BackColor = Color.Blue;

(and clear it when the selection changes)

I would have thought that Selected = true would also do the 'backcolor\highlighting' that happens through the mouse click. Am I missing something?

+1  A: 

Has the control got focus? If not the default setting is to hide the selection when the control doesn't have focus - see the HideSelection property.

Matt Breckon
Does setting the HideSelection=False and then giving a diff control focus keep the item's background as blue or is it that beige color (which I can barely see on my monitor)?
the empirical programmer
Removing focus from the listview changes the selection to a beige color on my Windows 7 machine - it is difficult to see I must admit.If you want to keep the selection blue you'll need to manually manage the selection state and set the background colour of the item.
Matt Breckon
+1  A: 

You do not need to handle the highlighting code yourself, but the item will only appear highlighted if the ListView control has focus. Add listView1.Select() after you select the item and see if that helps.

Otherwise, you'll need to set the HideSelection property on the ListView to false.

jheddings