How can I force a ListView to disregard clicking/not highlight a clicked item?

13,149

Solution 1

There is not any property to disable item selection in the ListView.

What you can do is to handle the event that notifies that an item has been selected by attaching an event handler to ItemSelectionChanged and then deselect the item:

yourListView.ItemSelectionChanged += yourListView_ItemSelectionChanged;

private void yourListView_ItemSelectionChanged(
    object sender,
    ListViewItemSelectionChangedEventArgs e)
{
   if (e.IsSelected)
      e.Item.Selected = false;
}

Solution 2

To add to Adriano Repettis soloution, I had something similar where I grayed out the items I wanted to block, his solution prevents the blue highlight but the Item still has focus which causes a problem when the item's backcolor is set to anything but white since some of the selected line becomes white. to handle this, I suggest adding the line:

e.Item.Focused = false;

the final code:

yourListView.ItemSelectionChanged += yourListView_ItemSelectionChanged;

private void yourListView_ItemSelectionChanged(
    object sender,
    ListViewItemSelectionChangedEventArgs e)
{
    if (e.Item.BackColor == Color.LightGray)
    {
        e.Item.Selected = false;
        e.Item.Focused = false;
    }
}

Solution 3

The solution can cause flicker. You might want to trap mousedown and mouseup events. In mousedown you disable redraw of the listview by either sending WM_SETREDRAW message to the listview with 0 wParam or by calling LockWindowUpdate passing the window handle of the listview. Ex

SendMessage(listview.Handle, WM_SETREDRAW, 0, 0);

Or

LockWindowUpdate(listview.Handle);

In mouseup you will enable redraw by either of the methods below.

SendMessage(listview.Handle, WM_SETREDRAW, 1, 0);

Or

LockWindowUpdate(IntPtr.Zero);

...and, you don't have to invalidate the listview right away. It will do it by itself when necessary.

Take note that you have to do dllimports in this method.

Do you think that's overkill?

Here's another:

extend the ListView class, override WndProc, wait for the WM_NOTIFY to be reflected to the control, check for the first four bytes of the m.LParam if it is -100 and set m.Result = (IntPtr)1 if it is and don't pass the message to base.WndProc.

-100 is LVN_ITEMCHANGING. Returning 1 or 0 via m.Result will disallow or allow the change to take place. The first four bytes of m.LParam is the notification code, the next four bytes the identification of the control sending the message and the 3rd fout bytes is the window handle of the control sending the message. For more information see NMHDR and NMLISTVIEW structures. Google is your friend.

Late answer, though. You might think writing this long took me years to finish.

Share:
13,149
B. Clay Shannon-B. Crow Raven
Author by

B. Clay Shannon-B. Crow Raven

My novel about climate change and social justice featuring talking animals traveling through time and space to prevent disasters is now available on amazon, in three formats: Taterskin & The Eco Defenders Kindle eBook; Taterskin & The Eco Defenders Paperback; Taterskin & The Eco Defenders Hardcover Taterskin & The Eco Defenders, told in “first canine” by the titular character, a Labrador Retriever, is the story of a few humans and several talking animals who travel through time and space to make the past—and thus the future—a better place. The improvements effected by the Eco Defenders benefit not just the earth itself, but also mistreated humans and animals. In Book 1 (“Wonders Never Cease”), The Eco Defenders travel 150 million years into the past, to meet a Pterodactyl and make plans to “nip Nazism in the bud.” After that, it's on to 1787 Australia to protect the indigenous people and the environment there. The Eco Defenders next go to India, where they assemble animals from all over that country to put an end to Thuggee and fights to the death between Cobras and Mongooses. Their final stop is 1885 Africa, where the Eco Defenders band together with the local animals to prevent King Leopold of Belgium from taking control of the Congo, following which they put an end to the poaching of animals throughout the continent. Book 2 (“Tell it to Future Generations”) takes up with the Eco Defenders following up on their earlier adventures by 1) Preventing the American Civil War in 1861, after which a slave they free joins them; 2) Saving the Indians from being massacred at Wounded Knee in 1890, following which Chapawee, a Sioux Indian, joins the Eco Defenders; 3) Putting an end to the practice of vivisection (experimentation on live animals) in 1903; 4) Coming to the aid of exploited workers in 1911 Manhattan, saving hundreds from the Triangle Shirtwaist Fire; and 5) Traveling to the Amazon Basin in 1978 to protect and preserve the Amazon rainforest. @@@@@@@@@@@@@@@@@@@@@@@ I have lived in eight states; besides my native California (where I was born and where I now again reside), in chronological order I have infested: New York (Brooklyn), Montana (Helena), Alaska (Anchorage), Oklahoma (Bethany), Wisconsin (New Berlin and Oconomowoc), Idaho (Coeur d'Alene), and Missouri (Piedmont). I am a writer of both fiction (for which I use the nom de guerre "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006 and can be downloaded gratis here.

Updated on June 14, 2022

Comments

  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven about 2 years

    As I'm not processing the items in a ListView based on what a user may select (the processing always uses everything in the list), I want to disallow the selecting of an item which may make the user think he is limiting the processing to that one item (I've already got multiselect = false, so that's not an issue).