C#:如何避免双击事件时发生 TreeNode 检查
所以我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这是 TreeView 中的一个错误(http://social.msdn.microsoft.com/Forums/en-US/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:
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:
我使用以下代码对其进行管理,这可以防止检查根节点:
I managed it with the following code, which prevents checking root nodes:
尝试扩展 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.