TreeView in Winform is a common and powerful control. Generally, you may be familiar with it. It is estimated that most of us are doing node locating, this method is usually used for loop search. The method is as follows:
Foreach (TreeNode node in this. TreeView1.Nodes)
{
If (node. Text = "Name of the node to be found ")
{
Return node;
}
Return null;
}
Another method is to use TreeNode. to illustrate how to use the FromHandle method to quickly locate nodes, we assume that there is a group in which there are many companies and each company has some subsidiaries,
We need to add some device nodes or personnel nodes under each subsidiary. So how should we deal with the display and positioning of these nodes?
First, we record the tree node information of these companies and their subsidiaries when adding nodes to the group and put them in the memory list. The Code is as follows.
TreeView treeView = this. TreeView1;
TreeView. Nodes. Clear ();
Foreach (string key in groupList. Keys)
{
TreeNode node = treeView. Nodes. Add (key, key, 0 );
If (! CompanyHandleDict. ContainsKey (key ))
{
CompanyHandleDict. Add (key, node. Handle );
}
}
Foreach (string key in groupList. Keys)
{
Foreach (string subKey in groupList [key]. Keys)
{
TreeNode node = TreeNode. FromHandle (treeView, companyHandleDict [key]);
If (node! = Null)
{
TreeNode tempNode