TreeView Scroll Position Get and Set

sanjoydas

New member
Joined
Nov 12, 2008
Messages
4
Programming Experience
3-5
I used a treeview with scrollable property true. Firstly I generated the treeview. Then I selected a treenode as :
Code:
dim node as TreeNode
node=treeview1.Nodes(1)
After that I refresh the treeview in a structured way, like Expanded node as expanded and Collapsed as collapsed.
Then I need to keep that selected node in the visible zone, so I used :
Code:
node.EnsureVisible()
Still the node is not is not in the visible zone as the vertical Scroll Bar goes at the top of the treeview.
Please help how I can maintain the vertical Scroll Bar position?

Thanks in Advance.
Sanjoy.:)
 

sanjoydas

New member
Joined
Nov 12, 2008
Messages
4
Programming Experience
3-5
Thank you for your suggestion. The Code looks like follow :

Code:
Dim node as TreeNode
Dim TreeView As TreeViewData
node=treeview1.Nodes(1)
TreeView = New TreeViewData(treeview1)
treeview1.Nodes.Clear
'in a loop I regenerate the nodes and sub nodes again and attach it to the Treeview
Dim nd as TreeNode
nd.Text="N1"
treeview1.Nodes.Add(nd)
'end loop
TreeView.PopulateTree(treeview1)
node.EnsureVisible()
'Where Treeviewdata is like following :-
Code:
<Serializable()> Public Structure TreeViewData
Public Nodes() As TreeNodeData
Public Sub New(ByVal treeview As TreeView)
Try
    If treeview.Nodes.Count = 0 Then Exit Sub
    ReDim Nodes(treeview.Nodes.Count - 1)
    For i As Integer = 0 To treeview.Nodes.Count - 1
         Nodes(i) = New TreeNodeData(treeview.Nodes(i))
    Next
    Catch ex As Exception
    End Try
End Sub

Public Sub PopulateTree(ByVal treeview As TreeView)
Try
     If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit Sub
          treeview.BeginUpdate()
          For i As Integer = 0 To Me.Nodes.Length - 1
               Call Me.Nodes(i).ExpandTreeNode(treeview.Nodes(i))
          Next
          treeview.EndUpdate()
      Catch ex As Exception
      End Try
End Sub

End Structure
Code:
    <Serializable()> Public Structure TreeNodeData
        Public Text As String
        Public Name As String
        Public Expanded As Boolean
        Public Tag As Object
        Public Nodes() As TreeNodeData

        Public Sub New(ByVal node As TreeNode)
            Try
                Me.Text = node.Text
                Me.Name = node.Name
                Me.Expanded = node.IsExpanded
                If (Not node.Tag Is Nothing) AndAlso node.Tag.GetType.IsSerializable Then Me.Tag = node.Tag
                If node.Nodes.Count = 0 Then
                    Me.Expanded = True
                    Exit Sub
                End If
                ReDim Nodes(node.Nodes.Count - 1)
                For i As Integer = 0 To node.Nodes.Count - 1
                    If node.Nodes(i).Level < 2 Then
                        Nodes(i) = New TreeNodeData(node.Nodes(i))
                    End If
                Next
            Catch ex As Exception
            End Try
        End Sub

        Public Sub ExpandTreeNode(ByVal tnod As TreeNode)
            Try
                If tnod.Level = 0 Then
                    If tnod.Text = Me.Text AndAlso tnod.Name = Me.Name AndAlso tnod.Tag = Me.Tag Then
                        If Me.Expanded Then tnod.Expand()
                    End If
                Else
                    Dim tnodparent As TreeNode = tnod.Parent
                    For j As Integer = 0 To tnodparent.Nodes.Count - 1
                        If tnodparent.Nodes(j).Text = Me.Text AndAlso tnodparent.Nodes(j).Name = Me.Name AndAlso tnodparent.Nodes(j).Tag = Me.Tag Then
                            If Me.Expanded Then tnodparent.Nodes(j).Expand()
                            Exit For
                        End If
                    Next
                End If

                If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit Sub
                For i As Integer = 0 To Me.Nodes.Length - 1
                    If tnod.Nodes.Count > i Then
                        Call Me.Nodes(i).ExpandTreeNode(tnod.Nodes(i))
                    End If
                Next
            Catch ex As Exception
             End Try
        End Sub

    End Structure
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
I've added CODE tags to your post for readability. Please do so yourself in future.

The problem is here:
Code:
[COLOR="Red"]node=treeview1.Nodes(1)[/COLOR]
TreeView = New TreeViewData(treeview1)
[COLOR="SeaGreen"]treeview1.Nodes.Clear[/COLOR]
'in a loop I regenerate the nodes and sub nodes again and attach it to the Treeview
Dim nd as TreeNode
nd.Text="N1"
treeview1.Nodes.Add(nd)
'end loop
TreeView.PopulateTree(treeview1)
[COLOR="Blue"]node.EnsureVisible()[/COLOR]
You first of all get a reference to a TreeNode from the TreeView. You then clear the TreeView, i.e. you remove all nodes from it, including the one you just got a reference to. After repopulating the TreeView you try to ensure that the node you previously got a reference to is visible. How can that happen though, if you removed that node from the tree?

You really should not be removing all the nodes and then repopulating. That's very inefficient, plus it leads to problems like the one you're having now. You should simply be adding and/or removing specific nodes as needed and leaving the rest as they are.
 
Top Bottom