views:

27

answers:

1

How do I make a listview not automatically check an item when I double click it?

I can try hooking into the MouseDoubleClick event, and set the Checked property to false, but that feels like a bit of an hack. I also run a reasonably expensive calculation when an item is actually checked, and don't want this code to run on a double click. With the event hooking above, the ItemCheck & ItemChecked events are raised before the double click is handled.

Is there an elegent solution to this?

+1  A: 

Elegant isn't typically the word that jumps to mind when you have to hack the way the native Windows control works, but that's what required here. Do consider if you really want your control to behave differently from the listviews in any other program.

Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

using System;
using System.Windows.Forms;

class MyListView : ListView {
    protected override void WndProc(ref Message m) {
        // Filter WM_LBUTTONDBLCLK
        if (m.Msg != 0x203) base.WndProc(ref m);
    }
}
Hans Passant
Hmmm, this is the least hacky way to do it.... this works, especially if you call the OnMouseDoubleClick method from WndProc when the message is hit. This bypasses the Check handling, while keeping the double click event which is what I wanted to do. Thanks!
Gareth