Search code examples
c#recursiontreeviewregistrytreenode

Filter Tree-View with similar word on the TreeNode.Text C#


I need to filter Tree on Winforms. basically the Tree View contain the list from the registry with all the key's on the branch

now,when i run the method to search some values in all the tree, the result I get is just part of the tree and I cant save the branch were connected from the result to the root. there is any way to save the hierarchy that in the end the result will showed correctly. ?

I tried to put it on Dictionary that contains string with the level,index, and full path. any idea?

this is the search code.the Dictionary basically to show the results. for testing

Dictionary<string, TreeNode> Result = new Dictionary<string, TreeNode>();  

private void SearchforNodes(TreeNodeCollection nodes)
    {
        bool x = true;

        while (x)
        {
            foreach (TreeNode item in nodes)
            {
                x = ReadAllKeys(item);
            }
        }

    }
    bool flag = true;
    private bool ReadAllKeys(TreeNode node)
    {


        foreach (TreeNode item in node.Nodes)
        {
            if (item.Nodes.Count > 0)
            {
                ReadAllKeys(item);
            }
            else
            {
                var result = SearchKey(item);
                if (result != null)
                {
                    if (!Result.Keys.Contains(string.Format("Index: {0} level: {1} Text: {2}  FullPathTree: {3}  ", result.Index, result.Level, result.Text, result.FullPath)))
                    {
                        Result.Add(string.Format("Index: {0} level: {1} Text: {2}  FullPathTree: {3}  ", result.Index, result.Level, result.Text, result.FullPath), result);
                        flag = false;

                    }
                    else
                    {
                        flag = false;
                    }

                }
            }
        }

        return flag;
    }

    private TreeNode SearchKey(TreeNode node)
    {
        if (node.Text.ToUpper().Contains(txtSearch.Text.ToUpper()))
        {
            return node;
        }
        else
        {
            return null;
        }

    }

Solution

  • For what I remember in using the Treeviews, in the class/classes used to contain the data used for the tree we usually added a Parent or a ParentID field so that each node until the leaf knows who is it's parent. this is useful to set which nodes other than the leaf must be visible. Our data class was usually in a datatable or a collection but also always put in the tag element of the treenode to be able to have access to all information from the node. (the Tag is an object so you can attach your classes or datarows and then retrieve them with a cast)

    If you just use the standard node element in the tree as your data source, I'm sure the node has a Parent information so that you can add to the collection of the visible elements you want to display after filtering all the elements up to the root.

    Another thing I found useful when working with tree data is to have a plain collection of all the nodes for example a List updated when building the tree and, use that collection to set the visibility of the node and its parents, ancestors and so on.

    I'm not sure this is exactly what you need but I hope it can set you to the right path.