创建一个类 TreeView2
namespace System.Windows.Forms
{
public class TreeView2 : TreeView
{
protected override void WndProc(ref Message m)
{
if (m.Msg != 0x203)
{
base.WndProc(ref m);
}
}
}
}
然后前台放置一个TreeView控件。名叫:treeView1
后台代码:
private void Form3_Load(object sender, EventArgs e)
{
//测试数据
TreeNode t = new TreeNode();
t.Text = "所有权限";
TreeNode t1 = new TreeNode();
t1.Text = "会员消费";
t.Nodes.Add(t1);
for (int i = 0; i < 100; i++)
{
TreeNode t2 = new TreeNode();
t2.Text = "Test" + i;
t1.Nodes.Add(t2);
}
this.treeView1.CheckBoxes = true;
this.treeView1.Nodes.Add(t);
}
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
foreach (TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if (node.Nodes.Count > 0)
{
this.CheckAllChildNodes(node, nodeChecked);
}
}
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action != TreeViewAction.Unknown)
{
if (e.Node.Nodes.Count > 0)
{
this.CheckAllChildNodes(e.Node, e.Node.Checked);
}
}
}
最后,将Form3.Designer.cs 文件中的 TreeView 全部替换成我们的 TreeView2