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

Feature request: FindNodebyKey

7 Answers 197 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Marek
Top achievements
Rank 1
Marek asked on 02 May 2007, 09:57 AM
Hi
It would be useful to be able to add nodes to the tree, specifying a key and then to be able to locate that node with a call along the lines of:

RadTreeNode node = radTreeView.GetNodeByKey(key);

Best regards

Marek

7 Answers, 1 is accepted

Sort by
0
Boyko Markov
Telerik team
answered on 02 May 2007, 12:26 PM
Hello Marek,

Thank you for the proposal. The feature will be present in the SP1 of RadControls for WinForms in a couple of weeks.
If you have more ideas do not hesitate to share them with us.






Greetings,
Ray
the telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ian
Top achievements
Rank 1
answered on 29 Feb 2008, 04:46 AM
Hi
Has this feature been implemented yet? I can only see GetNodeByPath

Ian
0
Jordan
Telerik team
answered on 29 Feb 2008, 08:20 AM
Hello Ian,

Thank you for writing.

This method was indeed implemented, but shortly after that it as replaced by the Find method of RadTreeNode Collection. This was done in order to provide better compatibility with the Windows Forms TreeView control.

Also, functionality allowing to execute arbitrary actions on a subtree of nodes in the form of commands has been added (the ExecuteBatchCommand and ExecuteScalarCommand static methods of RadTreeView). These methods can be used to search a subtree of nodes by executing a custom command for every node in that subtree.

This functionality is still relatively new, so any feedback will be greatly appreciated.

If you have any additional questions, please contact me.

Hope that helps.

Greetings,
Jordan
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ian
Top achievements
Rank 1
answered on 02 Mar 2008, 06:40 AM
Thanks for that

It would helpful if you could give a usage example on how to find a node by key. I am using the tag property of the nodes to store either a DataRow value or an array and I see the parameter for the value to search in Find is a string.

Also the two commands you describe look interesting but also would like an example of their use

Either VB or C#

Many thanks

Ian
0
Boyko Markov
Telerik team
answered on 04 Mar 2008, 09:35 AM
Hello Ian,

Thank you for writing back. Here are the details you requested:

  1. We should set the Name property of RadTreeNode as it is used for comparison in the Find method:
     
        private void SetNodesNames()     
            {     
                Stack<RadTreeNode> stack = new Stack<RadTreeNode>();     
                RadTreeNode node = null;     
                int depth = 1;     
                   
                int i;     
                for (i = this.radTreeView1.Nodes.Count - 1; i > -1; i--)     
                {     
                    node = this.radTreeView1.Nodes[i];     
                    if (node.Visible)     
                    {     
                        stack.Push(node);     
                    }     
                }     
        
                Stack<RadTreeNode> parentStack = new Stack<RadTreeNode>();     
                while (stack.Count > 0)     
                {     
                    node = stack.Pop();     
                    while (parentStack.Count > 0 && parentStack.Peek() != node.Parent)     
                    {     
                        parentStack.Pop();     
                        depth--;     
                    }     
        
                    node.Name = node.Text;     
                     
                 
                    if ((node.Nodes.Count > 0))     
                    {     
                        for (i = node.Nodes.Count - 1; i > -1; i--)     
                        {     
                            stack.Push(node.Nodes[i]);       
                        }     
                        parentStack.Push(node);     
                        depth++;     
                    }     
                }     
            }    
     

     
  2. Then you can use the following syntax to search for a node:
     
        RadTreeNode[] nodes = this.radTreeView1.Nodes.Find("Search Folders", true);

    The "Search Folders" string represents the name of a node to search for. We set "true" to allow searching in the subtrees.  

The ExecuteBatchCommand is used as follows:

    Telerik.WinControls.UI.
RadTreeView.ExecuteBatchCommand(this.radTreeView1.Nodes, -1, RadTreeNode.ExpandCollapseCommand, true);
 
There are a few default commands in RadTreeNode which you can use. If you would like you could create your own commands deriving the CommandBase class.

If you have any other questions please write us back.
 

Best wishes,
Boyko Markov
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Husam Al Madain
Top achievements
Rank 1
answered on 17 Mar 2011, 11:11 PM
the function find is not exist in q1 2011
radTreeView1.Nodes.Find(
"Search Folders"true); 

how can i get the node by name
0
Svett
Telerik team
answered on 18 Mar 2011, 05:34 PM
Hi Husam Al Madain,

There is a breaking change regarding this method. You can use the following code snippet to find a node by key:

private List<RadTreeNode> Find(string key, bool searchAllChildren, RadTreeNodeCollection treeNodeCollectionToLookIn)
{
    List<RadTreeNode> list = new List<RadTreeNode>();
 
    if ((treeNodeCollectionToLookIn == null) || (list == null))
    {
        return null;
    }
    for (int i = 0; i < treeNodeCollectionToLookIn.Count; i++)
    {
        if (treeNodeCollectionToLookIn[i] != null)
        {
            if (String.Compare(treeNodeCollectionToLookIn[i].Text, key, true) == 0)
            {
                list.Add(treeNodeCollectionToLookIn[i]);
            }
            if (searchAllChildren && (treeNodeCollectionToLookIn[i].Nodes.Count > 0))
            {
                list.AddRange(this.Find(key, searchAllChildren, treeNodeCollectionToLookIn[i].Nodes));
            }
        }
    }
    return list;
}

All the best,
Svett
the Telerik team
Tags
Treeview
Asked by
Marek
Top achievements
Rank 1
Answers by
Boyko Markov
Telerik team
Ian
Top achievements
Rank 1
Jordan
Telerik team
Husam Al Madain
Top achievements
Rank 1
Svett
Telerik team
Share this question
or