C#:如何避免在双击事件上发生TreeNode检查 [英] C#: How to avoid TreeNode check from happening on a double click event

查看:84
本文介绍了C#:如何避免在双击事件上发生TreeNode检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在C#Windows窗体应用程序中有一个TreeView。我需要的是一些节点被锁定,以便基于参数无法对其进行检查(或取消检查)。

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.

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

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

推荐答案

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

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

这篇关于C#:如何避免在双击事件上发生TreeNode检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆