Home All Groups Group Topic Archive Search About

Treeview windowstate save and restore

Author
2 Jun 2009 6:21 AM
Co
Hi All,

I have this Treeview of which I want to save the window status when
I'm doing some action with the nodes.
After the action I want to restore the windowstate.
I save the windowstate in a Dictionary. The code I have does work
except that it doesn't expand the nodes
under the root node. All nodes under there are collapsed or expanded
the way I saved them.

What do I need to also save the expand or collapse for the nodes under
the root node?

Private tt As New Dictionary(Of String, Boolean)

    Public Sub SaveTreeView(ByVal treeview As TreeView)

        For Each node As TreeNode In treeview.Nodes
            SaveTreeState(node)
        Next

    End Sub
    Public Sub RestoreTreeView(ByVal treeview As TreeView)

        For Each node As TreeNode In treeview.Nodes
            RestoreTreeViewExpandedState(node)
        Next

    End Sub
    Private Sub RestoreTreeViewExpandedState(ByVal objTreeNode _
                  As TreeNode)

        Dim objChildTreeNode As Node

        For Each objChildTreeNode In objTreeNode.Nodes
            Dim ID As String = objChildTreeNode.row("ID").ToString
            RestoreTreeViewExpandedState(objChildTreeNode)
            If tt.ContainsKey(ID) Then
                If tt(ID) Then
                    objChildTreeNode.Expand()
                Else
                    objChildTreeNode.Collapse()
                End If
            End If

        Next

    End Sub

    Private Function SaveTreeState(ByVal objTreeNode _
                  As TreeNode) As Dictionary(Of String, Boolean)

        Dim objChildTreeNode As Node

        For Each objChildTreeNode In objTreeNode.Nodes
            Dim ID As String = objChildTreeNode.row("ID").ToString
            SaveTreeState(objChildTreeNode)
            tt.Add(ID, objChildTreeNode.IsExpanded)
        Next

        Return tt
    End Function

Regards
Marco
The Netherlands

Author
2 Jun 2009 6:40 AM
Cor Ligthert[MVP]
See my reply to John some minutes earlier

Cor

Show quoteHide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:895df52b-ccef-4ac2-bca7-a5f52e7e4634@x3g2000yqa.googlegroups.com...
> Hi All,
>
> I have this Treeview of which I want to save the window status when
> I'm doing some action with the nodes.
> After the action I want to restore the windowstate.
> I save the windowstate in a Dictionary. The code I have does work
> except that it doesn't expand the nodes
> under the root node. All nodes under there are collapsed or expanded
> the way I saved them.
>
> What do I need to also save the expand or collapse for the nodes under
> the root node?
>
> Private tt As New Dictionary(Of String, Boolean)
>
>    Public Sub SaveTreeView(ByVal treeview As TreeView)
>
>        For Each node As TreeNode In treeview.Nodes
>            SaveTreeState(node)
>        Next
>
>    End Sub
>    Public Sub RestoreTreeView(ByVal treeview As TreeView)
>
>        For Each node As TreeNode In treeview.Nodes
>            RestoreTreeViewExpandedState(node)
>        Next
>
>    End Sub
>    Private Sub RestoreTreeViewExpandedState(ByVal objTreeNode _
>                  As TreeNode)
>
>        Dim objChildTreeNode As Node
>
>        For Each objChildTreeNode In objTreeNode.Nodes
>            Dim ID As String = objChildTreeNode.row("ID").ToString
>            RestoreTreeViewExpandedState(objChildTreeNode)
>            If tt.ContainsKey(ID) Then
>                If tt(ID) Then
>                    objChildTreeNode.Expand()
>                Else
>                    objChildTreeNode.Collapse()
>                End If
>            End If
>
>        Next
>
>    End Sub
>
>    Private Function SaveTreeState(ByVal objTreeNode _
>                  As TreeNode) As Dictionary(Of String, Boolean)
>
>        Dim objChildTreeNode As Node
>
>        For Each objChildTreeNode In objTreeNode.Nodes
>            Dim ID As String = objChildTreeNode.row("ID").ToString
>            SaveTreeState(objChildTreeNode)
>            tt.Add(ID, objChildTreeNode.IsExpanded)
>        Next
>
>        Return tt
>    End Function
>
> Regards
> Marco
> The Netherlands