Disabling a ListView in C#, but still showing the current selection

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?

17.08.2008 23:59:38
3 ОТВЕТА
РЕШЕНИЕ

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.

2
2.09.2008 21:12:48

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.

1
18.08.2008 01:01:35

Implement SelectedIndexChanged and do this

    private void listViewABC_SelectedIndexChanged(object sender, EventArgs e)
    {
        listViewABC.SelectedItems.Clear();
    }
0
7.07.2009 06:40:35