John Q

John Q

  • NA
  • 12
  • 44.2k

Searching for a Treeview and collapse it

Jan 22 2014 9:08 AM
I've a treeview and I want to search for a particular node everywhere in the treeview and when it finds it, it need to collapse it.  I've the following code but it doesn't seem to do the collapsing, if anyone has any idea please post some tips.  Thanks in advance

        private void FindByText()
        {
            TreeNodeCollection nodes = treeView1.Nodes;
            foreach (TreeNode n in nodes)
            {
                FindRecursive(n);
            }
        }

        private void FindRecursive(TreeNode treeNode)
        {
            string collapseStr = "";

            foreach (TreeNode tn in treeNode.Nodes)
            {
                collapseStr = tn.ToString();

                if (collapseStr.StartsWith("sub1"))              
                    tn.Collapse();
                FindRecursive(tn);
            }
        }

It needs to collapsing all the node = "sub1" below.

Node1
     sub1
          sub2
          sub3         
Node2
     sub1
          sub2
          sub3


Answers (2)