Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Treeview > RadTreeNode.Nodes.Clear() scrolls RadTreeView to first node

Answered RadTreeNode.Nodes.Clear() scrolls RadTreeView to first node

Feed from this thread
  • Clive avatar

    Posted on Oct 25, 2011 (permalink)

    Hi,

    When using a node's Nodes.Clear() method the tree view scrolls up to the top and shows the tree view's first node.

    If I load the Form2 and scroll down a bit so the first node is no longer visible, and check the check box, then double click on a node it will expand, and make the top node visible.

    My work around is to remove each node individually, i.e.
    If I load the Form2 and scroll down a bit so the first node is no longer visible, then double click on a node it will expand.

    Public Class Form2
        Inherits System.Windows.Forms.Form
      
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub
      
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
      
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.RadTreeView1 = New Telerik.WinControls.UI.RadTreeView()
            Me.CheckBox1 = New System.Windows.Forms.CheckBox()
            CType(Me.RadTreeView1, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'RadTreeView1
            '
            Me.RadTreeView1.BackColor = System.Drawing.SystemColors.Control
            Me.RadTreeView1.Cursor = System.Windows.Forms.Cursors.Default
            Me.RadTreeView1.Font = New System.Drawing.Font("Segoe UI", 8.25!)
            Me.RadTreeView1.ForeColor = System.Drawing.Color.Black
            Me.RadTreeView1.Location = New System.Drawing.Point(12, 11)
            Me.RadTreeView1.Name = "RadTreeView1"
            Me.RadTreeView1.RightToLeft = System.Windows.Forms.RightToLeft.No
            '
            '
            '
            Me.RadTreeView1.RootElement.ForeColor = System.Drawing.Color.Black
            Me.RadTreeView1.Size = New System.Drawing.Size(150, 250)
            Me.RadTreeView1.SpacingBetweenNodes = -1
            Me.RadTreeView1.TabIndex = 3
            Me.RadTreeView1.Text = "RadTreeView1"
            '
            'CheckBox1
            '
            Me.CheckBox1.AutoSize = True
            Me.CheckBox1.Location = New System.Drawing.Point(168, 23)
            Me.CheckBox1.Name = "CheckBox1"
            Me.CheckBox1.Size = New System.Drawing.Size(81, 17)
            Me.CheckBox1.TabIndex = 4
            Me.CheckBox1.Text = "CheckBox1"
            Me.CheckBox1.UseVisualStyleBackColor = True
            '
            'Form2
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(292, 273)
            Me.Controls.Add(Me.CheckBox1)
            Me.Controls.Add(Me.RadTreeView1)
            Me.Name = "Form2"
            Me.Text = "Form2"
            CType(Me.RadTreeView1, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)
            Me.PerformLayout()
      
        End Sub
        Friend WithEvents RadTreeView1 As Telerik.WinControls.UI.RadTreeView
      
      
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            For i = 0 To 20
                Dim node = RadTreeView1.Nodes.Add("node " + i.ToString)
                For j = 0 To 20
                    Dim n2 = node.Nodes.Add("node " + i.ToString + " " + j.ToString)
                Next
            Next
            RadTreeView1.LazyMode = True
        End Sub
      
      
        Private Sub RadTreeView1_NodeExpandedChanged(sender As System.Object, e As Telerik.WinControls.UI.RadTreeViewEventArgs) Handles RadTreeView1.NodeExpandedChanged
            Dim n = RadTreeView1.SelectedNode
            If n Is Nothing Then Return
            If CheckBox1.Checked Then
                n.Nodes.Clear()
            Else
                While n.Nodes.Count > 0
                    n.Nodes.RemoveAt(0)
                End While
            End If
            For i = 0 To 10
                Dim node = n.Nodes.Add("Expando " + i.ToString)
            Next
        End Sub
    End Class

    My experience with .Net is 7 years. My experience with Rad is about 2 weeks.

    • OS version and applied service packs

    Microsoft Windows Server 2003, Standard Edition, Service Pack 2

    • Regional and language settings, if different from En-US

    English (Australia)

    • .NET version (.NET2, .NET3, .NET3.5)

    .NET 4.0.30319 SP1Rel, Visual Studio 2010 Version 10.0.40219.1 SP1Rel

    • Exact version of the Telerik product 

    Runtime Version v2.0.50727, Version 2011.2.11.831


    Reply

  • Answer Svett Svett admin's avatar

    Posted on Oct 27, 2011 (permalink)

    Hi Clive,

    The best way is to use the NodeExpandedChanging event in this way:

        Private Sub RadTreeView1_NodeExpandedChanging(sender As System.Object, e As Telerik.WinControls.UI.RadTreeViewCancelEventArgs) Handles RadTreeView1.NodeExpandedChanging
     
            Dim selectedNode = RadTreeView1.SelectedNode
     
            If selectedNode Is Nothing Then Return
     
            Me.RadTreeView1.TreeViewElement.BeginUpdate()
     
            If CheckBox1.Checked Then
                selectedNode.Nodes.Clear()
            Else
                While selectedNode.Nodes.Count > 0
                    selectedNode.Nodes.RemoveAt(0)
                End While
            End If
     
            For i = 0 To 10
                Dim node = selectedNode.Nodes.Add("Expando " + i.ToString)
            Next
     
            Me.RadTreeView1.TreeViewElement.EndUpdate(False, Telerik.WinControls.UI.RadTreeViewElement.UpdateActions.ItemRemoved)
        End Sub
    End Class

    I hope this helps. 

    Regards,
    Svett
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

    Reply

  • Posted on Feb 1, 2012 (permalink)

    Hi,

    Is there a public issue tracked on this?  Or Nodes.Clear() expected to always select the first node in the tree?  I'd like to vote on it if it there is an issue.

    -Adam

    Reply

  • Svett Svett admin's avatar

    Posted on Feb 3, 2012 (permalink)

    Hi Adam,

    You can vote for the feature request here.

    Regards,
    Svett
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

    Reply

  • Alex avatar

    Posted on Feb 15, 2012 (permalink)

    I have a similar problem. Every call of Node.Clear() scrolls TreeView to first node. This is thery annoying. Telerik version is 2011.3.11.1219. How can I at least remember and restore first visible node, before (I hope) you fix this?

    Reply

  • Svett Svett admin's avatar

    Posted on Feb 17, 2012 (permalink)

    Hello Alex,

    You can save the scroll position and get it back after nodes are cleared. You can use the following code snippet as a sample:
    int scrollValue = this.radTreeView1.VScrollBar.Value;
    int maximum = this.radTreeView1.VScrollBar.Maximum;
     
    this.radTreeView1.Nodes[0].Nodes[0].Nodes[9].Nodes.Clear();
     
    this.radTreeView1.VScrollBar.Value = scrollValue - (int)Math.Abs(this.radTreeView1.VScrollBar.Maximum - maximum);

    Greetings,
    Svett
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Treeview > RadTreeNode.Nodes.Clear() scrolls RadTreeView to first node
Related resources for "RadTreeNode.Nodes.Clear() scrolls RadTreeView to first node"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]