Finding TreeView.Node by text

I'm using C# .NET 2.0.  I add a string to a TreeView:

Treeview1.Nodes.Add("myNode");

I'd then like to see if the above entry is in the Node list:

if(Treeview1.Nodes.Find("myNode", false).Length.Equals(0))
...

but Find() doesn't match on the string.  The first parameter of Find() is a string key.  However, TreeNodes don't have a key property and if Find isn't getting my node out by the string, what is it looking for?
brettrAsked:
Who is Participating?
 
Babycorn-StarfishCommented:
Hi,

try something along the lines of:

 public Boolean FindNodeByText(TreeNode tn, String match)
        {
            if (tn.Text.Equals(match))
                return true;
            foreach (TreeNode t in tn.Nodes)
            {    
                {
                    Boolean b = (FindNodeByText(t, match));
                    if (b)
                        return true;
                }
            }    
            return false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(FindNodeByText(treeView1.TopNode, "TestToFind") + "");
        }
0
 
NeotkCommented:
Can't you do this?
TreeView1.FindNode("nodestring");

Regards, Rodrigo Leote
0
 
brettrAuthor Commented:
Where did you get FindNode?  That's not a method on the TreeView.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
brettrAuthor Commented:
@ Babycorn-Starfish

Once I'm in the foreach loop, it doesn't go into the method body.  I get false whether the match is made or not.  There is never a return of true.
0
 
Babycorn-StarfishCommented:
can you check in your debugger whether tn has nodes, e.g tn.Nodes.Count > 0
0
 
NeotkCommented:
Strange, as a TreeView object, i have the method FindNode =/
0
 
brettrAuthor Commented:
>>can you check in your debugger whether tn has nodes, e.g tn.Nodes.Count > 0

Ah!  No - it doesn't.  That's just a single node so it doesn't have any nodes.  However, the parent treeview does have 64 nodes.  I modified the code to use a TreeNodeCollection:

        private Boolean FindNodeByText(TreeNodeCollection tn, String match)
        {
            bool success = false;

            foreach (TreeNode t in tn)
            {
                {
                    if(t.Text.Equals(match))
                        success = true;
                }
            }
            return success;
        }


use as:

if (!FindNodeByText(mytreeview.Nodes, __text))
                mytreeview.Nodes.Add(__text);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.