前几天在木野狐老大的园子里逛,见了新出的贴子《一个快捷访问 TreeView 控件节点的帮助类》,受到其中的代码启发。借老大的代码过来。自己改了改,将节点遍历的过程分离出来。做成了两个虚函数。我们可以在遍历的过程中。做更多的工作,而我们使用的方法,就是继承这个类。并重写这两个虚函数就可以,基于本人是菜鸟级的原因,代码可能不是很好。请各位前辈不吝指教。
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Windows.Forms.ComponentModel;
5using System.Windows.Forms;
6namespace TreeNodeCatch
7{
8 public class TreeNodeAccess
9 {
10 private TreeView _TreeView;
11 private TreeNode _TreeNode;
12 private List<TreeNode> nodes;
13 private delegate void SetEachNode(TreeNode tn);
14 private event SetEachNode OnGetEachNode;
15 private delegate void EndEachAction();
16 private event EndEachAction OnGetAllNode;
17 protected TreeNodeAccess() { }
18 public TreeNodeAccess(TreeView treeview)
19 {
20 _TreeView = treeview;
21 }
22 public TreeNodeAccess(TreeNode treenode)
23 {
24 _TreeNode = treenode;
25 }
26 private void Walk(TreeNode node)
27 {
28 if (node == null)
29 {
30 nodes.Add(_TreeView.Nodes[0]);
31 foreach (TreeNode childNode in _TreeView.Nodes[0].Nodes)
32 {
33 Walk(childNode);
34 }
35 }
36 else
37 {
38 nodes.Add(node);
39 foreach (TreeNode childNode in node.Nodes)
40 {
41 Walk(childNode);
42 }
43 }
44 }
45 private IEnumerable<TreeNode> GetAllTreeNodes()
46 {
47 nodes = new List<TreeNode>();
48 Walk(null);
49 foreach (TreeNode node in nodes)
50 {
51 yield return node;
52 }
53 }
54 private IEnumerable<TreeNode> GetAllTreeNodes(TreeNode TreeNode)
55 {
56 nodes = new List<TreeNode>();
57 Walk(TreeNode);
58 foreach (TreeNode node in nodes)
59 {
60 yield return node;
61 }
62 }
63 public virtual int MaxLen
64 {
65 get
66 {
67
68 List<TreeNode> Tmp = nodes;
69 Tmp.Sort(delegate(TreeNode a, TreeNode b) { return -a.Level.CompareTo(b.Level); });
70 int Max = Tmp[0].Level;
71 Tmp = null;
72 return Max;
73 }
74 }
75 private void Action(TreeNodeAccess ClassName)
76 {
77 IEnumerable<TreeNode> nodes;
78 if (_TreeView != null)
79 {
80 nodes = GetAllTreeNodes();
81 }
82 else
83 {
84 nodes = GetAllTreeNodes(_TreeNode);
85 }
86 foreach (TreeNode node in nodes)
87 {
88 if (OnGetEachNode != null)
89 {
90 OnGetEachNode(node);
91 ClassName.OnGetEachNode(node);
92 }
93 }
94 if (OnGetAllNode!= null)
95 {
96 OnGetAllNode();
97 ClassName.OnGetAllNode();
98 }
99 }
100
101 public bool SearchNode(string value,SearchMode Mode)
102 {
103 TreeNode Res;
104 TreeView MyTree;
105 if (_TreeNode != null)
106 {
107 MyTree =_TreeNode.TreeView;
108 }
109 else
110 {
111 MyTree = _TreeView;
112 }
113 switch (Mode)
114 {
115 case SearchMode.ByText:
116 {
117 Res = nodes.Find(delegate(TreeNode tn) { return tn.Text ==value; });
118 break;
119 }
120 case SearchMode.ByName:
121 {
122 Res = nodes.Find(delegate(TreeNode tn) { return tn.Name == value ; });
123 break;
124 }
125 default:
126 {
127 Res = null;
128 break;
129 }
130 }
131 if (Res != null)
132 {
133 MyTree.SelectedNode = Res;
134 Res.Parent.Expand();
135 return true;
136 }
137 else
138 {
139 return false;
140 }
141 }
142 public virtual void Execute(TreeNode Node){}
143 public virtual void FinishGetNode(){}
144 public void Load(TreeNodeAccess ClassName)
145 {
146 OnGetAllNode += new EndEachAction(FinishGetNode);
147 OnGetEachNode += new SetEachNode(Execute);
148 ClassName.OnGetAllNode += new EndEachAction(ClassName.FinishGetNode);
149 ClassName.OnGetEachNode += new SetEachNode(ClassName.Execute);
150 Action(ClassName);
151 }
152 }
153 public enum SearchMode
154 {ByText,ByName}
155}
156
2using System.Collections.Generic;
3using System.Text;
4using System.Windows.Forms.ComponentModel;
5using System.Windows.Forms;
6namespace TreeNodeCatch
7{
8 public class TreeNodeAccess
9 {
10 private TreeView _TreeView;
11 private TreeNode _TreeNode;
12 private List<TreeNode> nodes;
13 private delegate void SetEachNode(TreeNode tn);
14 private event SetEachNode OnGetEachNode;
15 private delegate void EndEachAction();
16 private event EndEachAction OnGetAllNode;
17 protected TreeNodeAccess() { }
18 public TreeNodeAccess(TreeView treeview)
19 {
20 _TreeView = treeview;
21 }
22 public TreeNodeAccess(TreeNode treenode)
23 {
24 _TreeNode = treenode;
25 }
26 private void Walk(TreeNode node)
27 {
28 if (node == null)
29 {
30 nodes.Add(_TreeView.Nodes[0]);
31 foreach (TreeNode childNode in _TreeView.Nodes[0].Nodes)
32 {
33 Walk(childNode);
34 }
35 }
36 else
37 {
38 nodes.Add(node);
39 foreach (TreeNode childNode in node.Nodes)
40 {
41 Walk(childNode);
42 }
43 }
44 }
45 private IEnumerable<TreeNode> GetAllTreeNodes()
46 {
47 nodes = new List<TreeNode>();
48 Walk(null);
49 foreach (TreeNode node in nodes)
50 {
51 yield return node;
52 }
53 }
54 private IEnumerable<TreeNode> GetAllTreeNodes(TreeNode TreeNode)
55 {
56 nodes = new List<TreeNode>();
57 Walk(TreeNode);
58 foreach (TreeNode node in nodes)
59 {
60 yield return node;
61 }
62 }
63 public virtual int MaxLen
64 {
65 get
66 {
67
68 List<TreeNode> Tmp = nodes;
69 Tmp.Sort(delegate(TreeNode a, TreeNode b) { return -a.Level.CompareTo(b.Level); });
70 int Max = Tmp[0].Level;
71 Tmp = null;
72 return Max;
73 }
74 }
75 private void Action(TreeNodeAccess ClassName)
76 {
77 IEnumerable<TreeNode> nodes;
78 if (_TreeView != null)
79 {
80 nodes = GetAllTreeNodes();
81 }
82 else
83 {
84 nodes = GetAllTreeNodes(_TreeNode);
85 }
86 foreach (TreeNode node in nodes)
87 {
88 if (OnGetEachNode != null)
89 {
90 OnGetEachNode(node);
91 ClassName.OnGetEachNode(node);
92 }
93 }
94 if (OnGetAllNode!= null)
95 {
96 OnGetAllNode();
97 ClassName.OnGetAllNode();
98 }
99 }
100
101 public bool SearchNode(string value,SearchMode Mode)
102 {
103 TreeNode Res;
104 TreeView MyTree;
105 if (_TreeNode != null)
106 {
107 MyTree =_TreeNode.TreeView;
108 }
109 else
110 {
111 MyTree = _TreeView;
112 }
113 switch (Mode)
114 {
115 case SearchMode.ByText:
116 {
117 Res = nodes.Find(delegate(TreeNode tn) { return tn.Text ==value; });
118 break;
119 }
120 case SearchMode.ByName:
121 {
122 Res = nodes.Find(delegate(TreeNode tn) { return tn.Name == value ; });
123 break;
124 }
125 default:
126 {
127 Res = null;
128 break;
129 }
130 }
131 if (Res != null)
132 {
133 MyTree.SelectedNode = Res;
134 Res.Parent.Expand();
135 return true;
136 }
137 else
138 {
139 return false;
140 }
141 }
142 public virtual void Execute(TreeNode Node){}
143 public virtual void FinishGetNode(){}
144 public void Load(TreeNodeAccess ClassName)
145 {
146 OnGetAllNode += new EndEachAction(FinishGetNode);
147 OnGetEachNode += new SetEachNode(Execute);
148 ClassName.OnGetAllNode += new EndEachAction(ClassName.FinishGetNode);
149 ClassName.OnGetEachNode += new SetEachNode(ClassName.Execute);
150 Action(ClassName);
151 }
152 }
153 public enum SearchMode
154 {ByText,ByName}
155}
156
其中Execute虚函数是遍历每一个节点时触发的过程。FineshGetNode是完成了遍历完所有节点后触发的过程,TreeNodeAccess类可以用TreeView和TreeNode来初始化。