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.
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Mike GoldRank #50 · 30.6K reputationPosted Jun 20, 2008, 9:56 AM EDT
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);
}
}
Patrick12 reputationPosted Jun 20, 2008, 10:06 AM EDT
Patrick12 reputationPosted Jun 20, 2008, 10:20 AM EDT