问题描述
以下代码直接从Microsoft在。
The following code is taken direct from Microsoft at http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aftercheck%28VS.80%29.aspx.
// Updates all child tree nodes recursively.
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
foreach (TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if (node.Nodes.Count > 0)
{
// If the current node has child nodes, call the CheckAllChildsNodes method recursively.
this.CheckAllChildNodes(node, nodeChecked);
}
}
}
// NOTE This code can be added to the BeforeCheck event handler instead of the AfterCheck event.
// After a tree node's Checked property is changed, all its child nodes are updated to the same value.
private void node_AfterCheck(object sender, TreeViewEventArgs e)
{
// The code only executes if the user caused the checked state to change.
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node.Nodes.Count > 0)
{
/* Calls the CheckAllChildNodes method, passing in the current
Checked value of the TreeNode whose checked state changed. */
this.CheckAllChildNodes(e.Node, e.Node.Checked);
}
}
}
包含一个treeview并调用node_AfterCheck(惊喜,惊喜),树视图AfterCheck事件。然后,它递归检查或取消选中树视图上的子节点。
You put it in a form containing a treeview and call node_AfterCheck on (surprise, surprise), the treeview AfterCheck event. It then recursively checks or unchecks the child nodes on the treeview.
然而,如果你真的尝试了它,并且在同一个树视图复选框上多次点击,节点最终与其父检查不同步。你可能需要几个级别的孩子,可能有100个孩子,UI更新速度足够慢,可以注意到这种情况。
However if you actually try it, and click several times on the same treeview check box fast enough, the child nodes end up with their check out-of-sync with the parent. You probably need a couple of levels of children with perhaps 100 children in-total for the UI update to be slow enough to notice this happening.
我试过一对夫妇的事情(例如在node_AfterCheck开始时禁用treeview控件,并在结束时重新启用),但是不同步的问题仍然发生。
I've tried a couple of things (such as disabling the treeview control at the beginning of node_AfterCheck and re-enabling at the end), but the out-of-sync problem still happens.
Any
推荐答案
.NET TreeView类为原生Windows控件大量自定义鼠标处理,以合成Before / After事件。不幸的是,他们没有得到它很正确。当您开始快速点击时,您会生成双击讯息。原生控件通过切换项的选中状态来响应双击,不告诉.NET封装器。你不会得到一个Before / AfterCheck事件。
The .NET TreeView class heavily customizes mouse handling for the native Windows control in order to synthesize the Before/After events. Unfortunately, they didn't get it quite right. When you start clicking fast, you'll generate double-click messages. The native control responds to a double-click by toggling the checked state for the item, without telling the .NET wrapper about it. You won't get a Before/AfterCheck event.
这是一个错误,但他们不会修复它。解决方法并不困难,您需要防止本机控件看到双击事件。向项目中添加一个新类,并粘贴以下代码。编译。从工具箱顶部删除新控件,替换现有的控件。
It's a bug but they won't fix it. The workaround is not difficult, you'll need to prevent the native control from seeing the double-click event. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox, replacing the existing one.
using System;
using System.Windows.Forms;
class MyTreeView : TreeView {
protected override void WndProc(ref Message m) {
// Filter WM_LBUTTONDBLCLK
if (m.Msg != 0x203) base.WndProc(ref m);
}
}
这篇关于Winforms树视图,递归检查子节点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!