Home All Groups Group Topic Archive Search About

How to select treenode in treeview by name?

Author
4 Apr 2005 9:27 PM
Wes
I have a treeview that I am continually added nodes to.  Each time a new node
is added I would like to select that node.

Is this possible?  I have looked at selectedNode, but I don't know how to
point that at a given node using the nodes name. 

I have the nodes name and it's parents name.  Does anyone know a good way to
find the node based on this information?

Thanks a bunch.

Author
4 Apr 2005 9:51 PM
Mattias Sjögren
>I have a treeview that I am continually added nodes to.  Each time a new node
>is added I would like to select that node.

The easiest way to do that is probably

TreeNode node = // ... create node via Nodes.Add or whatever
yourTreeView.SelectedNode = node;


>Is this possible?  I have looked at selectedNode, but I don't know how to
>point that at a given node using the nodes name. 
>
>I have the nodes name and it's parents name.  Does anyone know a good way to
>find the node based on this information?

TreeNodes don't have names. I'm not sure if you mean the name of the
variable you use to reference the nodes or the Text property of the
node. Either way, it's probably easier to do as above.



Mattias

--
Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Are all your drivers up to date? click for free checkup

Author
4 Apr 2005 10:29 PM
Wes
Thanks for the response.

When I said name before I did mean the treeNode.text and treeNode.parent.text.

My question is how do I use the above mentioned information to get a new
treeNode object that I can select.

For Example

TreeNode myTreeNode = "how do I create treenode based on parent.text and    
     node.text?"
myTreeView.selectedNode = myTreeNode;


Show quoteHide quote
"Mattias Sjögren" wrote:

>
> >I have a treeview that I am continually added nodes to.  Each time a new node
> >is added I would like to select that node.
>
> The easiest way to do that is probably
>
> TreeNode node = // ... create node via Nodes.Add or whatever
> yourTreeView.SelectedNode = node;
>
>
> >Is this possible?  I have looked at selectedNode, but I don't know how to
> >point that at a given node using the nodes name. 
> >
> >I have the nodes name and it's parents name.  Does anyone know a good way to
> >find the node based on this information?
>
> TreeNodes don't have names. I'm not sure if you mean the name of the
> variable you use to reference the nodes or the Text property of the
> node. Either way, it's probably easier to do as above.
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>
Author
5 Apr 2005 6:44 PM
Mattias Sjögren
>When I said name before I did mean the treeNode.text and treeNode.parent.text.

Ah, ok.


>My question is how do I use the above mentioned information to get a new
>treeNode object that I can select.

If all nodes have unique Text properties, you could maintain a
Hashtable that maps Text to TreeNode and use that to look up the
parent node.



Mattias

--
Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
5 Apr 2005 8:52 PM
David Lei
I wrote two functions for a project that I did that find the TreeNode base
on Node.Text or Node.Tag.

TopNode - Node you want the search to start

public static TreeNode TreeNodeFindTag(TreeNode TopNode, string Tag)
{
//Recursive find
foreach(TreeNode node in TopNode.Nodes)
{
if(node.Tag.ToString() == Tag)
return node;


//Search its childrens
TreeNode nodeChild = TreeNodeFindTag(node,Tag);
if(nodeChild != null) return nodeChild;
}
return null;
}

public static TreeNode TreeNodeFindText(TreeNode TopNode, string Text)
{
foreach(TreeNode node in TopNode.Nodes)
{
if(node.Text.Trim().ToUpper() == Text.Trim().ToUpper())
return node;
//Recursive find
TreeNode nodeChild = TreeNodeFindText(node,Text);
if(nodeChild != null) return nodeChild;
}
return null;
}

David

"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:%23Kl839gOFHA.1176@TK2MSFTNGP12.phx.gbl...
> >When I said name before I did mean the treeNode.text and
treeNode.parent.text.
Show quoteHide quote
>
> Ah, ok.
>
>
> >My question is how do I use the above mentioned information to get a new
> >treeNode object that I can select.
>
> If all nodes have unique Text properties, you could maintain a
> Hashtable that maps Text to TreeNode and use that to look up the
> parent node.
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.

Bookmark and Share