Another way to locate the TreeView node Treenode.fromhandle

Source: Internet
Author: User
Keywords We these each time as follows
Tags control find group key keys name return string

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.