Find Nodes in a TreeView
I want to find all nodes in a tree view that contain a string. I can find whole word matches but not partial.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
PatrickPosted Jun 20, 2008, 2:20 PM
PatrickPosted Jun 20, 2008, 2:06 PM
Mike GoldPosted Jun 20, 2008, 1:56 PM
just recurse through the treenodes and test for the string using IndexOf
private void TestNode(TreeNode treeNode, string testString)
{
// test node
if (treeNode.Text.IndexOf(testString) >= 0)
MessageBox.Show("fount text");
// Print each node recursively.
foreach (TreeNode tn in treeNode.Nodes)
{
TestNode(tn);
}
}
// Call the procedure using the TreeView.
private void CallRecursive(TreeView treeView)
{
// Print each node recursively.
TreeNodeCollection nodes = treeView.Nodes;
foreach (TreeNode n in nodes)
{
TestNode(n, testString);
}
}