Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My tree view populated by folders and files. And I have used below code for filtering tree view but it does not return all the files that match string, just it returns one file.
For example letter "f" there is in 3 files but when I search it returns just 1 file.

C#
<pre>private TreeNode FindNodeByValue(TreeNodeCollection nodes, string searchstring)
    {
       
        // Loop through the tree node collection
        foreach (TreeNode node in nodes)
        {
            // Does the value match the search string?
            if (node.Value.ToUpper().Contains (searchstring.ToUpper()))
                // Yes it does match - return it
                return node;
            else
            {
                // No it does not match - search any child nodes of this node
                TreeNode childNode = SearchChildNodes(node, searchstring);
                // If the childNode is not null it was a match
                if (childNode != null)
                    // Return the matching node
                    return childNode;
            }
        }
        // If the matching node is not found return null
        return null;
    }

    /// <summary>
    /// This method searches a node's ChildNodes collection to find a matching value
    /// with the incoming search string
    /// It will iteratively call itself as it drills into each nodes child nodes (if present)
    /// </summary>
    /// <param name="parentNode">Parent node to search for a match</param>
    /// <param name="searchstring">string to be matched with the Nodes Value property</param>
    /// <returns>Treenode of the matching node if found.  If not found it will be null</returns>
    private TreeNode SearchChildNodes(TreeNode parentNode, string searchstring)
    {
        // Loop through the child nodes of the parentNode passed in
        foreach (TreeNode node in parentNode.ChildNodes)
        {
            // Does the value match the search string?
            if (node.Value.ToUpper().Contains(searchstring.ToUpper()))
                // Yes it does match - return it
                return node;
            else
            {
                // No it does not match - recursively search any child nodes of this node
                TreeNode childNode = SearchChildNodes(node, searchstring);
                // If the childNode is not null it was a match
                if (childNode != null)
                    // Return the matching node
                    return childNode;
            }
        }
        // If the matching node is not found OR if there were no child nodes then return null
        return null;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TreeNode trnode=FindNodeByValue(TreeView1.Nodes, fieldFilterTxtBx.Text);
        if (trnode != null)
        {
            TreeView1.Nodes.Clear();
          //  TreeNode newnode = new TreeNode("Detail Engineering");
           // TreeView1.Nodes.Add(newnode);
            TreeView1.Nodes.Add(trnode);
            TreeView1.ExpandAll();
        }
        else

{

    Label1.Text = "No file found";

}
    }
Posted
Updated 25-Nov-12 5:50am
v2

1 solution

The "return" keyword exits the scope of the method, so your code will always return only the first match found. What you need to do is create List<treenode> before the foreach statement, and instead of returning the matching node, add it to the list and continue looping. return the list (possibly empty) at the bottom of the foreach loop. You can thin iterate the list of matching nodes.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900