Winform inside the TreeView is a common, powerful control, the general use of everyone may be more familiar with, we estimate that most of us do the positioning node, is generally the use of a circular lookup method. As shown in the following method
foreach (TreeNode node in this. Treeview1.nodes)
{
If node. Text = "Node name to find")
{
return node;
}
return null;
}
Another way is to make
Using the Treenode.fromhandle method to quickly locate a node, in order to illustrate how to use it, we assume that there is a scenario where there is a group, there are many companies in the group, each company has some subsidiaries,
We need to add some device nodes or staff nodes below each subsidiary. So how do we handle the display and positioning of these nodes?
First of all, when we add the company and subsidiary nodes of the group, we record the tree node information of these companies and subsidiaries, and put them in the memory list. The code looks like this.
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 = node. Nodes.Add (subkey, subkey, 0);
if (!subcompanyhandledict.containskey (key + subkey))
{
Subcompanyhandledict.add (key + subkey, Tempnode.handle);
}
}
}
}
After recording the handle of these TreeNode, we can quickly locate them if we want to add a child node to those nodes. The code looks like this.
foreach (Taxi Taxi in taxidcit.values)
{
string key = Taxi. Myinfo.corporation + Taxi. Myinfo.filialy;
TreeNode node = treenode.fromhandle (TreeView, Subcompanyhandledict[key]);
if (node!= null)
{
TreeNode subnode = new TreeNode (Taxi. Mybrand, 1, 2);
Subnode.tag = "vehicle";
Node. Nodes.Add (subnode);
}
The above is just a little trick to handle the location of the node, I hope to help.