Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to search within a Tree

Posted on 2010-11-24
3
Medium Priority
?
394 Views
Last Modified: 2012-05-10
Is it possible to search for a treenode within in tree by the treenode text?  I have a windows form application C# application that allows users to create new tree nodes and assign ID as text.  I need to make sure the ID value they assign is unique within the parent.  

For example,

Tree
   - Category 1   (ID = 001)
      - SubCat 1   (ID = 031)
      - SubCat 2   (ID = 091)
      - SubCat 3   (ID =   ---->  If the user trys to enter 091, 031 or 001 for the ID, then I need to                                           display an error.  How can I search within a tree?
  -  Category 2   (ID = 004)

           
0
Comment
Question by:Hojoformo
3 Comments
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 34208583
use embedded for loops to access the trees within the trees.

compare their values.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 34208601
When you add the TreeNodes, use the ID value as the KEY:
http://msdn.microsoft.com/en-us/library/57aa8e09.aspx

Then you can use the Nodes.Find() method to determine if a node with that key already exists:
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenodecollection.find.aspx
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1600 total points
ID: 34208987
Honestly, I would do another way. I would use a dictionary to store references to all nodes, and use id as aq key:

The idea is outlined in code below.

The main reason for that is performance. Also, it's quite handy - using an id you may find any node very quickly. Search in dictionaries MUCH faster than in trees. For big trees this might be important.My approach does not consume much additional memory.

Drawback - you need to maintain bot collections - your tree and the dictionary.
TreeView tv = new TreeView();
            Dictionary<int, TreeNode> nodeHash = new Dictionary<int, TreeNode>();

            TreeNode node = new TreeNode();
            int someNewID = 5;
            
            if(!nodeHash.ContainsKey(someNewID))
            {
                node.Text = someNewID.ToString();
                tv.Nodes.Add(node);
                nodeHash.Add(someNewID, node);
            }
            {
                MessageBox.Show("This ID already exists");
            }

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

916 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