TreeView ignore double click only at checkbox

Option 1: Completely disable the double click event.
Create a customer control

class MyTreeView : TreeView
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0203)
        {
            m.Result = IntPtr.Zero;
        }
        else
        {
            base.WndProc(ref m);
        }
    }
}

and in your designer file ( form.Designer.cs ), look for where the control was created, and replace the call to TreeView constructor with your new control.

this.treeView1 = new MyTreeView();

Option 2: Treat a double click event as two single click events

class MyTreeView : TreeView
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0203)
        {
            m.Msg = 0x0201;
        }
        base.WndProc(ref m);
    }
}

Personally I think option 2 is more intuitive. When user clicks the check box twice, the checkbox is not checked.


I found this question when googling for the same bug. The problem with NoodleFolk's solution is that it disables expanding the three by double clicking on an item. By combining NoodleFolk's answer with john arlens answer, you would get something like this:

class NewTreeView : TreeView
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x203) // identified double click
        {
            var localPos = PointToClient(Cursor.Position);
            var hitTestInfo = HitTest(localPos);
            if (hitTestInfo.Location == TreeViewHitTestLocations.StateImage)
                m.Result = IntPtr.Zero;
            else
                base.WndProc(ref m);
        }
        else base.WndProc(ref m);
    }
}

I (quickly) tested this solution, and it seems to work.


If you just want to know a DoubleClick event occurred from the CheckBox:

private void TreeViewDoubleClick(object sender, EventArgs e)
{
    var localPosition = treeView.PointToClient(Cursor.Position);
    var hitTestInfo = treeView.HitTest(localPosition);
    if (hitTestInfo.Location == TreeViewHitTestLocations.StateImage) 
        return;

    // ... Do whatever other processing you want
}

Combining the above answers, I found this to be the best solution for me. Double clicking on a node to expand its children still works, only double clicking on a checkbox is affected and fixed:

class MyTreeView : TreeView
{
    protected override void WndProc(ref Message m)
    {
      if (m.Msg == 0x0203 && this.CheckBoxes)
      {
        var localPos = this.PointToClient(Cursor.Position);
        var hitTestInfo = this.HitTest(localPos);
        if (hitTestInfo.Location == TreeViewHitTestLocations.StateImage)
        {
          m.Msg = 0x0201;
        }
      }
      base.WndProc(ref m);
    }
}