GetNodeByKey method doesn't appear to work

4 posts, 0 answers
  1. Phi
    Phi avatar
    19 posts
    Member since:
    Jun 2007

    Posted 01 Jul 2007 Link to this post

    Hi,

    I want to know how to use the GetNodeByKey() method.
    It doesn't appear to work, so can you tell me if I'm missing something.

    Here is the code I use:

    RadTreeNode node = new RadTreeNode("node1");
    node.Key = 1;
    this.radTreeView1.Nodes.Add(node);

    node = new RadTreeNode("node2");
    node.Key = 2;
    this.radTreeView1.Nodes.Add(node);


    // Won't be able to find the node with key = 2, messagebox will appear
    RadTreeNode selected = this.radTreeView1.GetNodeByKey(2);
    if (selected == null)
     MessageBox.Show("couldnt find selected node with key=2 using GetNodeByKey()");

    // this should find it ok.
    foreach (RadTreeNode radNode in this.radTreeView1.Nodes)
    {
     if (Convert.ToInt32(radNode.Key) == 2)
      MessageBox.Show("Found node with key = 2");
    }

    Regards,
    Phi Le

  2. Boyko Markov
    Admin
    Boyko Markov avatar
    610 posts

    Posted 03 Jul 2007 Link to this post

    Hi Phi,

    The GetNodeByKey method is obsolete at present. It's purpose was to have some value associated with a node. You should use the Tag property of RadTreeNode to do what you need.

    If I could be of further assistance, please write back.
     

    Kind regards,
    Ray
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Phi
    Phi avatar
    19 posts
    Member since:
    Jun 2007

    Posted 03 Jul 2007 Link to this post

    Hi Ray,

    We do use the Tag property currently, it is useful to attach extra information to a node. In this case however, I wanted a way to uniquely identify a node, and be able to quickly access it from the treeview.

    I look forward to the GetNodeByKey() method being implemented in future versions as I believe it can be a very useful. Also, it might be an idea to enforce uniqueness for the Key property for all nodes in the TreeView. I don't believe this happens at the moment.

    Cheers
    Phi
  5. Boyko Markov
    Admin
    Boyko Markov avatar
    610 posts

    Posted 04 Jul 2007 Link to this post

    Hello Phi,

    In this case I would suggest using the following method which accepts an int value as a parameter:

    public RadTreeNode GetNodeByKey(int key) 
            { 
                Queue<RadTreeNode> nodeQueue = new Queue<RadTreeNode>(); 
     
                for (int i = 0; i < this.radTreeView1.Nodes.Count; i++) 
                { 
                    RadTreeNode rootNode = this.radTreeView1.Nodes[i]; 
                    nodeQueue.Enqueue(rootNode); 
                } 
     
                while (nodeQueue.Count > 0) 
                { 
                    RadTreeNode node = nodeQueue.Dequeue(); 
     
                    if (node.Key != null
                    { 
                        if ((int)node.Key == key) 
                            return node; 
                    } 
     
                    for (int i = 0; i < node.Nodes.Count; i++) 
                    { 
                        RadTreeNode childNode = node.Nodes[i]; 
     
                        if (childNode.Key != null
                        { 
                            if ((int)childNode.Key == key) 
                                return childNode; 
                        } 
     
                        nodeQueue.Enqueue(childNode); 
                    } 
                } 
     
                return null
            } 

    As you see, I cast the key property of RadTreeNode to int value. If you want, you can pass a different type as a parameter of the GetNodeByKey method, and then cast the key property to that type.

    I hope this helps. We will be glad to hear any additional ideas on how we can improve this behavior.

     
    All the best,
    Ray
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top