建立一個類 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