This is a migrated thread and some comments may be shown as answers.

GetNodeByKey method doesn't appear to work

3 Answers 147 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Phi
Top achievements
Rank 1
Phi asked on 02 Jul 2007, 03:30 AM

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

3 Answers, 1 is accepted

Sort by
0
Boyko Markov
Telerik team
answered on 03 Jul 2007, 08:31 AM
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
0
Phi
Top achievements
Rank 1
answered on 03 Jul 2007, 11:59 PM
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
0
Boyko Markov
Telerik team
answered on 04 Jul 2007, 12:49 PM
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
Tags
Treeview
Asked by
Phi
Top achievements
Rank 1
Answers by
Boyko Markov
Telerik team
Phi
Top achievements
Rank 1
Share this question
or