Blorgbeard
Blorgbeard

Reputation: 103585

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?

Upvotes: 7

Views: 5453

Answers (3)

Mugunth
Mugunth

Reputation: 14509

Implement SelectedIndexChanged and do this

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

Upvotes: 0

Peter Hession
Peter Hession

Reputation: 499

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.

Upvotes: 2

Nick Berardi
Nick Berardi

Reputation: 54894

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.

Upvotes: 1

Related Questions