471,350 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

How do I add a treenode with a key?

I am trying to add nodes with keys to my treeview. I can add general
nodes without problem with:

//create new node
TreeNode newNode = new TreeNode(myIdNumber);

//create children
TreeNode myPersonNode = new TreeNode(myPerson);
TreeNode myAddressNode = new TreeNode(myAddress);

TreeNode[] myNodes = {myPersonNode, myAddressNode};

newNode.Nodes.AddRange(myNodes);

But now I want to interate throught them and stop when I am at a
certain node. I know I can iterate with:

private void PrintRecursive(TreeNode treeNode)
{
// adapted from msdn

//my nodes I'm testing for
if (treeNode.Index == 2 || treeNode.Index == 3)
MessageBox.Show(treeNode.Text);

// Print each node recursively.
foreach (TreeNode tn in treeNode.Nodes)
{

PrintRecursive(tn);
}
}

But using the index seems wrong. I read the docs where I can add a
key and text, but I can't figure out how to use it. I want to create
a node like above and when I add that node I want it to have a key so
I can find it with my recursive function. How can I find the node I
want besides using index? Thank you.
Jul 15 '08 #1
1 6609
On Jul 15, 10:38*pm, jmDesktop <needin4mat...@gmail.comwrote:
I am trying to add nodes with keys to my treeview. *I can add general
nodes without problem with:

* * * * * * //create new node
* * * * * * TreeNode newNode = new TreeNode(myIdNumber);

* * * * * * //create children
* * * * * * TreeNode myPersonNode = new TreeNode(myPerson);
* * * * * * TreeNode myAddressNode = new TreeNode(myAddress);

* * * * * * TreeNode[] myNodes = {myPersonNode, myAddressNode};

* * * * * * newNode.Nodes.AddRange(myNodes);

But now I want to interate throught them and stop when I am at a
certain node. *I know I can iterate with:

* * * * private void PrintRecursive(TreeNode treeNode)
* * * * {
* * * * * * // adapted from msdn

* * * * * * //my nodes I'm testing for
* * * * * * if (treeNode.Index == 2 || treeNode.Index == 3)
* * * * * * * * MessageBox.Show(treeNode.Text);

* * * * * * // Print each node recursively.
* * * * * * foreach (TreeNode tn in treeNode.Nodes)
* * * * * * {

* * * * * * * * PrintRecursive(tn);
* * * * * * }
* * * * }

But using the index seems wrong. *I read the docs where I can add a
key and text, but I can't figure out how to use it. *I want to create
a node like above and when I add that node I want it to have a key so
I can find it with my recursive function. *How can I find the node I
want besides using index? *Thank you.
If you want to associate some custom value with a TreeNode, that's
precisely what TreeNode.Tag is for.
Jul 15 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by bclegg | last post: by
reply views Thread by fred | last post: by
1 post views Thread by AAaron123 | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.