tahir_666
asked on
C# TreeView Control - Find And Select
I have a tag value as string in my program....I want to search that Tag value in Tree control and want to select/expand the node with that Tag value. C# please!!!
ASKER
Code seems to have error....but I tried this
TreeNodeCollection Nodes = tvewNetworkSource.Nodes;
TreeNode[] Node = Nodes.Find(SectionID, true);
Node[0].Collapse();
....and No work.
TreeNodeCollection Nodes = tvewNetworkSource.Nodes;
TreeNode[] Node = Nodes.Find(SectionID, true);
Node[0].Collapse();
....and No work.
ASKER
SectionID is string having Tag value.
you would need to search recursively in each node's child
use this function
public TreeNode SearchTree(TreeNodeCollect ion nodes, string searchtext)
{
foreach(TreeNode node in nodes)
{
if(node.Tag as string == searchtext)
{
return node;
}
SearchTree(node.Nodes, searchtext);
}
}
and call it like
TreeNode tn = SearchTree( MyTreeView.Nodes, MySearchString);
//do whatever with tn
use this function
public TreeNode SearchTree(TreeNodeCollect
{
foreach(TreeNode node in nodes)
{
if(node.Tag as string == searchtext)
{
return node;
}
SearchTree(node.Nodes, searchtext);
}
}
and call it like
TreeNode tn = SearchTree( MyTreeView.Nodes, MySearchString);
//do whatever with tn
ASKER
I really appriciate ur effort dude...but I have searched the desired node using following code:
TreeNode[] n = this.tvewNetworkSource.Nod es.Find(Se ctionID,fa lse) ;
if (n.Length >0 )
{
//Problem
tvewNetworkSource.Selected Node = tvewNetworkSource.Nodes[0] ;
}
Now Im haveing problem in selecting the searched node. n in this case.
TreeNode[] n = this.tvewNetworkSource.Nod
if (n.Length >0 )
{
//Problem
tvewNetworkSource.Selected
}
Now Im haveing problem in selecting the searched node. n in this case.
TreeNodeCollection.Find seems to be new thing in .Net 2.0, the code above works for earlier version too :D
2.0 documentation says that 'Find' return treenodes whose 'Name' property matches the specified key, while you need to match tags. one more :D
2.0 documentation says that 'Find' return treenodes whose 'Name' property matches the specified key, while you need to match tags. one more :D
ASKER
Shit......UR rite :o)
try Node[0].Expand(); rather than Node[0].Collapse();
ASKER
So Can I have Code for Search and Selection for a Tag? :-P
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Accepted :o)
solution was working only at root level
please use this one
private TreeNode SearchTree(TreeNodeCollect ion nodes, string searchtext)
{
TreeNode n_found_node = null;
bool b_node_found = false;
foreach (TreeNode node in nodes)
{
if (node.Tag.ToString() as string == searchtext)
{
b_node_found = true;
n_found_node = node;
}
if (!b_node_found)
{
n_found_node = SearchTree(node.Nodes, searchtext);
}
}
return n_found_node;
}
please use this one
private TreeNode SearchTree(TreeNodeCollect
{
TreeNode n_found_node = null;
bool b_node_found = false;
foreach (TreeNode node in nodes)
{
if (node.Tag.ToString() as string == searchtext)
{
b_node_found = true;
n_found_node = node;
}
if (!b_node_found)
{
n_found_node = SearchTree(node.Nodes, searchtext);
}
}
return n_found_node;
}
TreeNodeCollection Nodes = TreeView.Nodes;
TreeNode Node = Nodes.Find("Node", true);
Node.Collapse();