TreeView节点选择影响其父子节点选中状态(递归)
来源:互联网 发布:怎样入驻淘宝导购 编辑:程序博客网 时间:2024/05/16 15:22
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 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 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 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 }
- TreeView节点选择影响其父子节点选中状态(递归)
- C# TreeView级联父子节点(选中子节点父节点也选中,反之选中父节点子节点也选中)
- treeview 父子节点联动
- 递归获取treeview选中父节点的所有子节点
- TreeView选择父节点自动选中子节点
- silverlight treeview 中添加复选框实现实现选择父节点,其下的子节点全部选中:
- TreeView控件的CheckBox自动实现父子节点递归选定
- TreeView父子联动效果保持节点状态一致
- TreeView父子联动效果保持节点状态一致
- TreeView父子联动效果保持节点状态一致
- VS2005 TreeView的checkBox的父子节点级联状态
- VS2005 TreeView的checkBox的父子节点级联状态
- TreeView父子联动效果保持节点状态一致
- TreeView父子联动效果保持节点状态一致
- 带checkbox的treeview父子节点的联动选择
- TreeView节点选中问题
- c#treeview选中节点
- Treeview 中1.选择父节点时子节点同时被选中 反之...... 2.每个节点只能选一个且不能影响其他项
- GCC ARM-inline-asm
- PHPMailer Sample
- X11介绍和/etc/X11/xorg.conf配置详解
- struts2 学习笔记- 传智播客 2010-9-8
- JTAG与ISP区别
- TreeView节点选择影响其父子节点选中状态(递归)
- 重新定义谷歌风格超快速自动搜索 - 施密特
- SAP 标准销售订单查看
- [随笔]面朝南京,我们都是孤臣孽子(作者:刘原)
- ajax各种属性
- 关于继承与组合的比较
- 在VC6.0下设置Unicode编译环境
- Sales order, Delivery and Invoice changes history tables
- Com中BSTR的使用