Solved

C# TreeView Control - Find And Select

Posted on 2006-06-22
12
38,023 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

628 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