![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace MyNetShop
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 /**事件:树形状结构节点单击时 Added By Wangxu 2010-2-25*/
20 private void treeView1_MouseClick(object sender, MouseEventArgs e)
21 {
22 //获取单击的节点
23 TreeNode root = treeView1.GetNodeAt(new Point(e.X, e.Y));
24 //该节点是否被选中
25 bool isChecked = root.Checked;
26
27 if (root != null)
28 {
29 //该节点的选中状态影响其子孙节点的状态
30 FooChild(root, isChecked);
31 //该节点的选中状态影响其父祖节点的状态
32 FooParent(root);
33 }
34 }
35
36 /**方法:递归子节点跟随其全选或全不选 Modify By Wangxu 2010-2-25*/
37 private void FooChild(TreeNode node, bool isChecked)
38 {
39 node.Checked = isChecked;
40 foreach (TreeNode nd in node.Nodes)
41 FooChild(nd, isChecked);
42 }
43
44 /**方法:递归父节点跟随其全选或全不选 Add By Wangxu 2010-2-25*/
45 private void FooParent(TreeNode root)
46 {
47 if (root.Parent != null)
48 {
49 //兄弟节点被选的个数
50 int brotherNodeCheckedCount = 0;
51 //遍历该节点的兄弟节点
52 foreach (TreeNode node in root.Parent.Nodes)
53 {
54 if (node.Checked == true)
55 brotherNodeCheckedCount += 1;
56 }
57 //兄弟节点全没选
58 if (brotherNodeCheckedCount == 0)
59 {
60 TreeNode parentNode = root.Parent;
61 parentNode.Checked = false;//其父节点也不选
62 FooParent(parentNode);
63 }
64 //兄弟节点中只要有一个被选
65 if (brotherNodeCheckedCount == 1)
66 {
67 TreeNode parentNode = root.Parent;
68 parentNode.Checked = true;
69 FooParent(parentNode);//其父节点也被选
70 }
71 }
72 }
73 }
74 }
75
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace MyNetShop
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 /**事件:树形状结构节点单击时 Added By Wangxu 2010-2-25*/
20 private void treeView1_MouseClick(object sender, MouseEventArgs e)
21 {
22 //获取单击的节点
23 TreeNode root = treeView1.GetNodeAt(new Point(e.X, e.Y));
24 //该节点是否被选中
25 bool isChecked = root.Checked;
26
27 if (root != null)
28 {
29 //该节点的选中状态影响其子孙节点的状态
30 FooChild(root, isChecked);
31 //该节点的选中状态影响其父祖节点的状态
32 FooParent(root);
33 }
34 }
35
36 /**方法:递归子节点跟随其全选或全不选 Modify By Wangxu 2010-2-25*/
37 private void FooChild(TreeNode node, bool isChecked)
38 {
39 node.Checked = isChecked;
40 foreach (TreeNode nd in node.Nodes)
41 FooChild(nd, isChecked);
42 }
43
44 /**方法:递归父节点跟随其全选或全不选 Add By Wangxu 2010-2-25*/
45 private void FooParent(TreeNode root)
46 {
47 if (root.Parent != null)
48 {
49 //兄弟节点被选的个数
50 int brotherNodeCheckedCount = 0;
51 //遍历该节点的兄弟节点
52 foreach (TreeNode node in root.Parent.Nodes)
53 {
54 if (node.Checked == true)
55 brotherNodeCheckedCount += 1;
56 }
57 //兄弟节点全没选
58 if (brotherNodeCheckedCount == 0)
59 {
60 TreeNode parentNode = root.Parent;
61 parentNode.Checked = false;//其父节点也不选
62 FooParent(parentNode);
63 }
64 //兄弟节点中只要有一个被选
65 if (brotherNodeCheckedCount == 1)
66 {
67 TreeNode parentNode = root.Parent;
68 parentNode.Checked = true;
69 FooParent(parentNode);//其父节点也被选
70 }
71 }
72 }
73 }
74 }
75