views:

1734

answers:

4

I have a ListView control, and I'm trying to figure out the easiest/best way to disallow changing the selected row(s), without hiding the selected row(s).

I know there's a HideSelection property, but that only works when the ListView is still enabled (but not focused). I need the selection to be viewable even when the ListView is disabled.

How can I implement this?

+1  A: 

There are two options, change the selected rows disabled colors. Or change all the other rows to simulate they are disabled except for the selected one. The first option is obviously the easiest, and the second option obviously is going to need some extra protections.

I have actually done the first option before and it works quite well. You just have to remember to change the colors back to the defaults in case another row is selected later on in the process.

Nick Berardi
A: 

Thanks Nick, that sounds like it will work for me - but how exactly do I change a row's disabled colors? All I could find is ListViewItem.BackColor, and that doesn't seem to work..

Blorgbeard
+1  A: 

You could also make the ListView ownerdraw. You then have complete control over how the items look whether they are selected or not or whether the ListView itself is enabled or not. The DrawListViewItemEventArgs provides a way to ask the ListView to draw individual parts of the item so you only have to draw the bits you're interested in. For example, you can draw the background of the item but leave it up to the ListView to draw the text.

Peter Hession
A: 

Implement SelectedIndexChanged and do this

    private void listViewABC_SelectedIndexChanged(object sender, EventArgs e)
    {
        listViewABC.SelectedItems.Clear();
    }
Mugunth Kumar