C#:如何避免双击事件时发生 TreeNode 检查

发布于 2024-11-09 10:55:04 字数 600 浏览 0 评论 0原文

所以我在 C# windows 窗体应用程序中有一个 TreeView 。我需要的是“锁定”某些节点,以便无法根据参数检查(或取消检查)它们。

我现在正在做的是这样的:

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;
    }
}

此代码在单击复选框时效果很好,但如果用户双击复选框,它仍然会选中/取消选中。

我尝试过使用 nodeMouseDoubleClick 事件,但这并没有真正帮助,因为我无法取消该事件...

是否有任何想法如何取消节点上的双击事件?...或其他什么? 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-11-16 10:55:04

我认为这是 TreeView 中的一个错误(http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/9d717ce0-ec6b-4758-a357-6bb55591f956/)。您需要对树视图进行子类化并禁用双击消息才能修复它。像这样:

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);
        }              
    };

当然,如果您这样做,您将不再能够在树视图中使用双击隐喻来执行其他操作(例如双击节点来启动属性页等)。

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).

天荒地未老 2024-11-16 10:55:04

如果您希望双击实际切换复选框,请尝试:

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);
}

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);
}
绮筵 2024-11-16 10:55:04

我使用以下代码对其进行管理,这可以防止检查根节点:

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;
}

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;
}
愁以何悠 2024-11-16 10:55:04

尝试扩展 TreeNode 类并添加一个布尔属性来维护正确的 checkState。这样,当有人双击节点时,您可以将该节点的选中状态重置回属性中存储的值。可能有更优雅的解决方案,但这是我能想到的最好的解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文