Solved

C# TreeView Control - Find And Select

Posted on 2006-06-22
12
37,466 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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