C# TreeView递归遍历方法

  /// <summary>
3 /// 查找TreeView控件下是否有指定值的节点
4 /// </summary>
5 /// <param name="tvControl"> TreeView控件 </param>
6 /// <param name="strValue"> 指定节点值 </param>
7 /// <returns></returns>
8   public static TreeNode FindNode(TreeView tvControl, string strValue)
9 {
10 TreeNode tnRet = null ;
11
12 foreach (TreeNode tn in tvControl.Nodes)
13 {
14
15 tnRet = FindNode(tn, strValue);
16
17 if (tnRet != null ) break ;
18
19 }
20
21 return tnRet;
22
23 }
24
25 /// <summary>
26 /// 查找当前节点下是否有指定值的节点
27 /// </summary>
28 /// <param name="tnParent"> 当前节点 </param>
29 /// <param name="strValue"> 指定节点值 </param>
30 /// <returns> TreeNode </returns>
31 public static TreeNode FindNode(TreeNode tnParent, string strValue)
32 {
33 if (tnParent == null ) return null ;
34
35 if (tnParent.Value == strValue) return tnParent;
36
37 TreeNode tnRet = null ;
38 TreeNode tnTemp = null ;
39
40 foreach (TreeNode tn in tnParent.ChildNodes)
41 {
42
43 tnTemp = FindNode(tn, strValue);
44
45 if (tnTemp != null && tnTemp.Value == strValue)
46 {
47 tnRet = tnTemp;
48 break ;
49 }
50
51 }
52
53 return tnRet;
54
55 }
56
57 /// <summary>
58 /// 查找TreeView控件下是否有指定值的节点
59 /// </summary>
60 /// <param name="tvControl"> TreeView控件 </param>
61 /// <param name="strValue"> 指定节点值 </param>
62 /// <returns></returns>
63 public static TreeNode FindNode(TreeView tvControl, string strValue)
64 {
65 TreeNode tnRet = null ;
66
67 foreach (TreeNode tn in tvControl.Nodes)
68 {
69
70 tnRet = FindNode(tn, strValue);
71
72 if (tnRet != null ) break ;
73
74 }
75
76 return tnRet;
77
78 }
79
80 /// <summary>
81 /// 查找当前节点下是否有指定值的节点
82 /// </summary>
83 /// <param name="tnParent"> 当前节点 </param>
84 /// <param name="strValue"> 指定节点值 </param>
85 /// <returns> TreeNode </returns>
86 public static TreeNode FindNode(TreeNode tnParent, string strValue)
87 {
88 if (tnParent == null ) return null ;
89
90 if (tnParent.Value == strValue) return tnParent;
91
92 TreeNode tnRet = null ;
93 TreeNode tnTemp = null ;
94
95 foreach (TreeNode tn in tnParent.ChildNodes)
96 {
97
98 tnTemp = FindNode(tn, strValue);
99
100 if (tnTemp != null && tnTemp.Value == strValue)
101 {
102 tnRet = tnTemp;
103 break ;
104 }
105
106 }
107
108 return tnRet;
109
110 }
111
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值