Solved

C# TreeView Control - Find And Select

Posted on 2006-06-22
12
37,143 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
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
Comment Utility
ok do

 TreeNodeCollection Nodes = TreeView.Nodes;

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

  Node.Collapse();
0
 

Author Comment

by:tahir_666
Comment Utility
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
Comment Utility
SectionID is string having Tag value.
0
 
LVL 5

Expert Comment

by:RoninThe
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:tahir_666
Comment Utility
Shit......UR rite :o)
0
 
LVL 11

Expert Comment

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

Author Comment

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

Accepted Solution

by:
RoninThe earned 300 total points
Comment Utility
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
Comment Utility
Accepted :o)
0
 

Expert Comment

by:C_sharp_beaver
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now