DoubleClick a listview Item – .NET Framework

  .net

Q(Question):

Hi all
I have a listview with checkboxes and I would like to catch the doubleclick event on an Item.
Everything is o.k, BUT, the double clicking on the item, also checks/unchecks the checkbox of the item, which I don’t want.

Can I work around this ?

Thanks In advance.

A(Answer):

You can create a derived class and override WndProc to bypass the WM_LBUTTONDBLCLK message:

public class MyListView : ListView
{
const int WM_LBUTTONDBLCLK = 0x203;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDBLCLK)
{
// raise the event
this.OnDoubleClick(EventArgs.Empty);
// but don't pass the message to base class
return;
}
base.WndProc(ref m);
}
}

Then open your Form.Designer.cs file and replace System.Windows.Forms.ListView with MyListView.

There are also some custom open-source controls like ObjectListView, some of which are a bit friendlier to use than ListView, and you have the source to customize it further to your needs.

LEAVE A COMMENT