开发者

C#: How to avoid TreeNode check from happening on a double click event

开发者 https://www.devze.com 2023-03-08 04:55 出处:网络
So I have a TreeView in a C# windows form app. What I need is for some nodes to be \"locked\" so that they cannot be checked (or unchecked), based on a parameter.

So I have a TreeView in a C# windows form app. What I need is for some nodes to be "locked" so that they cannot be checked (or unchecked), based on a parameter.

What I am doing now is this:

private void tv_local_BeforeCheck(object sender, TreeViewCancelEventArgs e) {
    TNode node = (TNode)开发者_运维知识库e.Node;
    //if a part node, cancel the action.
    if (node.Type == "Part") {
        e.Cancel = true;     
    }
    //if a locked node, cancel the action
    if (node.Locked == true) {
        e.Cancel = true;
    }
}

This code works great on a single click of the checkbox, but if the user double clicks on a checkbox, it still checks/unchecks.

I have tried playing with the nodeMouseDoubleClick event, but that doesnt really help, since I cannot cancel the event...

Is there any ideas out there how to cancel a double click event on a node?... or anything else? Thanks


This is a bug in the TreeView I think (http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/9d717ce0-ec6b-4758-a357-6bb55591f956/). You need to subclass the tree view and disable the double-click message in order to fix it. Like this:

public class NoClickTree : TreeView
    {
        protected override void WndProc(ref Message m)
        {
            // Suppress WM_LBUTTONDBLCLK
            if (m.Msg == 0x203) { m.Result = IntPtr.Zero; }
            else base.WndProc(ref m);
        }              
    };

Of course if you do this you'll no longer be able to use the double-click metaphor in the tree-view for other things (such as double click a node to launch a property page, or something).


If you want your double click to actually toggle the check box then try:

protected override void WndProc(ref Message m)
{
  // Filter WM_LBUTTONDBLCLK when we're showing check boxes
  if (m.Msg == 0x203 && CheckBoxes)
  {
    // See if we're over the checkbox. If so then we'll handle the toggling of it ourselves.
    int x = m.LParam.ToInt32() & 0xffff;
    int y = (m.LParam.ToInt32() >> 16) & 0xffff;
    TreeViewHitTestInfo hitTestInfo = HitTest(x, y);

    if (hitTestInfo.Node != null && hitTestInfo.Location == TreeViewHitTestLocations.StateImage)
    {
      OnBeforeCheck(new TreeViewCancelEventArgs(hitTestInfo.Node, false, TreeViewAction.ByMouse));
      hitTestInfo.Node.Checked = !hitTestInfo.Node.Checked;
      OnAfterCheck(new TreeViewEventArgs(hitTestInfo.Node, TreeViewAction.ByMouse));
      m.Result = IntPtr.Zero;
      return;
    }
  }

  base.WndProc(ref m);
}


I managed it with the following code, which prevents checking root nodes:

private void MyTreeView_MouseUp(object sender, MouseEventArgs e)
{
    // HACK: avoid to check root nodes (mr)
    var node = ((TreeView)sender).GetNodeAt(new Point(e.X, e.Y));
    if (node != null && node.Parent == null)
    BeginInvoke(new MouseEventHandler(TreeView_MouseUpAsync), sender, e);
}

private void TreeView_MouseUpAsync(object sender, MouseEventArgs e)
{
    if (IsDisposed)
        return;

    var node = ((TreeView)sender).GetNodeAt(new Point(e.X, e.Y));
    node.Checked = false;
}


Try extending the TreeNode class and add a boolean property that maintains the proper checkedState. That way, when someone double-clicks a node, you can reset the node's checked state back to the value stored in the property. There might be a more elegant solution, but this is the best I can think of.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号