Solved

C# TreeView Control - Find And Select

Posted on 2006-06-22
12
37,685 Views
Last Modified: 2011-04-14
I have a tag value as string in my program....I want to search that Tag value in Tree control and want to select/expand the node with that Tag value. C# please!!!
0
Comment
Question by:tahir_666
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16960088
ok do

 TreeNodeCollection Nodes = TreeView.Nodes;

 TreeNode Node = Nodes.Find("Node", true);

  Node.Collapse();
0
 

Author Comment

by:tahir_666
ID: 16960166
Code seems to have error....but I tried this

            TreeNodeCollection Nodes = tvewNetworkSource.Nodes;

            TreeNode[] Node = Nodes.Find(SectionID, true);

            Node[0].Collapse();

....and No work.
0
 

Author Comment

by:tahir_666
ID: 16960171
SectionID is string having Tag value.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:RoninThe
ID: 16960174
you would need to search recursively in each node's child
use this function

public TreeNode SearchTree(TreeNodeCollection nodes, string searchtext)
{
      foreach(TreeNode node in nodes)
      {
            if(node.Tag as string  == searchtext)
            {
                  return node;
            }
            SearchTree(node.Nodes, searchtext);
      }            
}

and call it like
TreeNode tn = SearchTree( MyTreeView.Nodes, MySearchString);
//do whatever with tn
0
 

Author Comment

by:tahir_666
ID: 16960230
I really appriciate ur effort dude...but I have searched the desired node using following code:

            TreeNode[] n = this.tvewNetworkSource.Nodes.Find(SectionID,false) ;

            if (n.Length >0 )
            {
               //Problem
              tvewNetworkSource.SelectedNode = tvewNetworkSource.Nodes[0];
            }

Now Im haveing problem in selecting the searched node. n in this case.
0
 
LVL 5

Expert Comment

by:RoninThe
ID: 16960254
TreeNodeCollection.Find seems to be new thing in .Net 2.0, the code above works for earlier version too :D
2.0 documentation says that 'Find' return treenodes whose 'Name' property matches the specified key, while you need to match tags. one more :D
0
 

Author Comment

by:tahir_666
ID: 16960320
Shit......UR rite :o)
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16960327
try Node[0].Expand(); rather than Node[0].Collapse();
0
 

Author Comment

by:tahir_666
ID: 16960335
So Can I have Code for Search and Selection for a Tag? :-P
0
 
LVL 5

Accepted Solution

by:
RoninThe earned 300 total points
ID: 16960430
doesn't the fuction i wrote in my first response work for searching?
posting it again here-
public TreeNode SearchTree(TreeNodeCollection nodes, string searchtext)
{
     foreach(TreeNode node in nodes)
     {
          if(node.Tag as string  == searchtext)
          {
               return node;
          }
          SearchTree(node.Nodes, searchtext);
     }          
}

as for selection-
TreeNode tn = SearchTree( MyTreeView.Nodes, MySearchString);
if( tn != null)
{
      MytreeView.SelectedNode = tn;//to select
      tn.Expand();//to expand
}

i havn't tested this, but this should work.



0
 

Author Comment

by:tahir_666
ID: 16960573
Accepted :o)
0
 

Expert Comment

by:C_sharp_beaver
ID: 23548744
solution was working only at root level
please use this one

private TreeNode SearchTree(TreeNodeCollection nodes, string searchtext)
        {
            TreeNode n_found_node = null;
            bool b_node_found = false;
            foreach (TreeNode node in nodes)
            {
                if (node.Tag.ToString() as string == searchtext)
                {
                    b_node_found = true;
                    n_found_node = node;
                }
                if (!b_node_found)
                {
                    n_found_node = SearchTree(node.Nodes, searchtext);
                }
            }
            return n_found_node;
        }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question