Scroll Treeview so Expanded node is on top

4 posts, 0 answers
  1. Ryan
    Ryan avatar
    10 posts
    Member since:
    Apr 2008

    Posted 14 Apr 2008 Link to this post

    Basically I have a RadTreeView structure with a lot of sub nodes.  I want to bea ble to scroll the treeview when a node is expanded so that the expanded node is the top node.  Ive been trying to use ScrollIntoView without much success.
  2. Jordan
    Admin
    Jordan avatar
    547 posts

    Posted 18 Apr 2008 Link to this post

    Hi Ryan,

    The current implementation of the ScrollIntoView method tries to scroll to the given node so that it is somewhere in the middle of the tree view control. However, this is not exactly the behavior that you need.

    You could try implementing that behavior with the following steps:
    1. Get the first visible node with code like:
      (this.radTreeView1.TreeViewElement.Items[0] as TreeNodeUI).AssociatedTreeNode
    2. Then use a depth first traversal to find the position of the first visible node and of the node that you want to scroll to.
    3. Finally scroll the tree view control with the difference between the row positions
    Hope this helps. I will follow up with some more code later.

    Best wishes,
    Jordan
    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. Jordan
    Admin
    Jordan avatar
    547 posts

    Posted 07 May 2008 Link to this post

    Hi Ryan,

    Sorry about the delay in getting back to you. Here's an implementation of the approach I proposed in my previous email:

    public class RadTreeViewScrollHelper 
        { 
            public static void ScrollToNode(Telerik.WinControls.UI.RadTreeView treeView, RadTreeNode node) 
            { 
                Telerik.WinControls.UI.RadTreeView.EnsureVisible(node); 
     
                RadTreeNode firstVisibleNode = (treeView.TreeViewElement.Items[0] as TreeNodeUI).AssociatedTreeNode; 
                int basePosition = RadTreeViewScrollHelper.GetPosition(treeView, firstVisibleNode); 
                int targetPosition = RadTreeViewScrollHelper.GetPosition(treeView, node); 
                int positionDelta = basePosition - targetPosition; 
                treeView.ScrollBy(positionDelta); 
            } 
     
            public static int GetPosition(Telerik.WinControls.UI.RadTreeView treeView, RadTreeNode targetNode) 
            { 
                int currentPosition = 0; 
     
                Stack<RadTreeNode> stack = new Stack<RadTreeNode>(); 
                RadTreeNode node = null
                int i; 
                for (i = treeView.Nodes.Count - 1; i > -1; i--) 
                { 
                    node = treeView.Nodes[i]; 
                    if (node.Visible) 
                    { 
                        stack.Push(node); 
                    } 
                } 
     
                while (stack.Count > 0) 
                { 
                    node = stack.Pop(); 
                    if (node == targetNode) 
                    { 
                        return currentPosition; 
                    } 
                    currentPosition++; 
     
                    if ((node.Nodes.Count > 0) && (node.Expanded)) 
                    { 
                        for (i = node.Nodes.Count - 1; i > -1; i--) 
                        { 
                            if (node.Nodes[i].Visible) 
                            { 
                                stack.Push(node.Nodes[i]); 
                            } 
                        } 
                    } 
                } 
     
                return -1; 
            } 
        } 

    And you can use it as shown below:

    private void button1_Click(object sender, EventArgs e) 
            { 
                RadTreeNode node = Telerik.WinControls.UI.RadTreeView.ExecuteScalarCommand(this.radTreeView1.Nodes, -1, 
                    new FindNodeByTextCommand(), this.textBox1.Text) as RadTreeNode; 
                if (node == null
                { 
                    return
                } 
     
                RadTreeViewScrollHelper.ScrollToNode(this.radTreeView1, node); 
            } 
     
            public class FindNodeByTextCommand : CommandBase 
            { 
                public override object Execute(params object[] settings) 
                { 
                    if (settings.Length > 1 && this.CanExecute(settings[0])) 
                    { 
                        RadTreeNode node = settings[0] as RadTreeNode; 
                        string text = (settings[1] as IList)[0] as string
     
                        if (node != null && !string.IsNullOrEmpty(text)) 
                        { 
                            if (node.Text == text) 
                            { 
                                return node; 
                            } 
                        } 
                    } 
                    return null
                } 
     
                public override bool CanExecute(object parameter) 
                { 
                    if (parameter is RadTreeNode) 
                    { 
                        return true
                    } 
                    return base.CanExecute(parameter); 
                } 
            } 

    I hope this helps. If you have any further questions, please contact me.

    Sincerely yours,
    Jordan
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Ryan
    Ryan avatar
    10 posts
    Member since:
    Apr 2008

    Posted 11 May 2008 Link to this post

    Perfect Thank you guys again for all your work.
Back to Top