Solved

How to search within a Tree

Posted on 2010-11-24
3
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

628 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