[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Finding TreeView.Node by text

Posted on 2007-07-30
7
Medium Priority
?
1,481 Views
Last Modified: 2013-11-07
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?
0
Comment
Question by:brettr
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:Neotk
ID: 19592799
Can't you do this?
TreeView1.FindNode("nodestring");

Regards, Rodrigo Leote
0
 

Author Comment

by:brettr
ID: 19592854
Where did you get FindNode?  That's not a method on the TreeView.
0
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 2000 total points
ID: 19592967
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:brettr
ID: 19593429
@ 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
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 19593448
can you check in your debugger whether tn has nodes, e.g tn.Nodes.Count > 0
0
 
LVL 4

Expert Comment

by:Neotk
ID: 19593712
Strange, as a TreeView object, i have the method FindNode =/
0
 

Author Comment

by:brettr
ID: 19593915
>>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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month20 days, 5 hours left to enroll

872 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