Click here to Skip to main content
14,692,581 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am find and highlighted tree node value using bellow code. I had to give full node value as search text.So i need to give part of the node value and highlighted all the node match to given text. Can some one help me.

MY code
private void button2_Click(object sender, EventArgs e)
{
FindByText();
}
private void FindByText()
       {
           TreeNodeCollection nodes = treeView1.Nodes;
           foreach (TreeNode n in nodes)
           {
               FindRecursive(n);
           }
       }

       private void FindRecursive(TreeNode treeNode)
       {
           foreach (TreeNode tn in treeNode.Nodes)
           {
               // if the text properties match, color the item
               if (tn.Text == this.textBox.Text)
               {
                   tn.BackColor = Color.Yellow;
                  
               }
               FindRecursive(tn);
           }
       }

Regards
Ruwan Atapattu
Posted
Updated 13-Aug-20 7:14am
v2
Comments
Sergey Alexandrovich Kryukov 25-Nov-13 16:21pm
   
What do you mean by "highlighting"? Selection? Anything else?
—SA

1 solution

You can use:
if (tn.Text.Contains(textBox1.Text))
{
    tn.BackColor = Color.Yellow;

}
I'd suggest you create a variable to hold the search string outside your recursive procedure to optimize the search ... if you are doing searches across large numbers of TreeNodes.

I observe that your code does not examine the root-level Nodes of the TreeView: is that a design decision ?
   
Comments
Member 14913784 13-Aug-20 13:27pm
   
Hi BillWoodruff,

When i tried using Contains it just allow to select one character and then it jump to that search letter in treeview,But i want to do like this way:

If i search by 'app' then it should give me result according to search text Example : 'Apple' and 'Application' and 'App' .

So how can i do this? Can you please suggest me solution for this i would appreciate.
BillWoodruff 14-Aug-20 7:09am
   
If you post a new question on QA, describing exactly what you want to happen, I will respond. Make sure you describe what nodes you want to find and color: all the tree nodes ? only child nodes of the current selected node ? only nodes at a certain level ?

Describe exactly what a "match" is: anywhere in the node text ?
Member 14913784 14-Aug-20 14:48pm
   
Yes,Anywhere in the node text and even it can be at any level
BillWoodruff 15-Aug-20 8:26am
   
Post a new question with the details I described above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900