Solved

C# TreeView Control - Find And Select

Posted on 2006-06-22
12
37,804 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

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…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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