C# TreeView递归遍历方法

  ///
3 /// 查找TreeView控件下是否有指定值的节点
4 ///

5 /// TreeView控件
6 /// 指定节点值
7 ///
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 ///
26 /// 查找当前节点下是否有指定值的节点
27 ///

28 /// 当前节点
29 /// 指定节点值
30 /// TreeNode
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 ///
58 /// 查找TreeView控件下是否有指定值的节点
59 ///

60 /// TreeView控件
61 /// 指定节点值
62 ///
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 ///
81 /// 查找当前节点下是否有指定值的节点
82 ///

83 /// 当前节点
84 /// 指定节点值
85 /// TreeNode
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

你可能感兴趣的:(C#与asp.net)