Geeks With Blogs

News


Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done

Save TreeView Nodes Expansion/Collapse & Checkboxes State In Session

Original article:

http://blog.binaryocean.com/2006/01/19/SaveTreeViewNodesExpansionCollapseStateCSAndVB.aspx

I added Checkboxes State save/restore into the class:

 

Imports Microsoft.VisualBasic
Imports System.Collections.Generic
 
Public Class TreeViewState
    Private RestoreTreeViewIndex As Integer
    Public Sub SaveTreeView(ByVal treeView As TreeView, ByVal key As String)
        Dim listExpanded As New List(Of Nullable(Of Boolean))
        Dim listChecked As New List(Of Nullable(Of Boolean))
        SaveTreeViewExpandedState(treeView.Nodes, listExpanded)
        SaveTreeViewCheckedState(treeView.Nodes, listChecked)
        HttpContext.Current.Session("1" + key + treeView.ID) = listExpanded
        HttpContext.Current.Session("2" + key + treeView.ID) = listChecked
    End Sub
 
    Private Sub SaveTreeViewExpandedState(ByVal nodes As TreeNodeCollection, ByVal list As List(Of Nullable(Of Boolean)))
        For Each node As TreeNode In nodes
            list.Add(node.Expanded)
            If node.ChildNodes.Count > 0 Then
                SaveTreeViewExpandedState(node.ChildNodes, list)
            End If
        Next
    End Sub
    Private Sub SaveTreeViewCheckedState(ByVal nodes As TreeNodeCollection, ByVal list As List(Of Nullable(Of Boolean)))
        For Each node As TreeNode In nodes
            Dim showCheckbox As Nullable(Of Boolean) = node.ShowCheckBox
            Dim isChecked As Boolean = False
            If showCheckbox.HasValue Then
                isChecked = CType(node.ShowCheckBox, Boolean) And node.Checked
                'If isChecked Then
                '    System.Diagnostics.Debug.Write("X")
                'End If
            End If
            list.Add(isChecked)
            If node.ChildNodes.Count > 0 Then
                SaveTreeViewCheckedState(node.ChildNodes, list)
            End If
        Next
    End Sub
 
 
    Public Sub RestoreTreeView(ByVal treeView As TreeView, ByVal key As String)
        Dim listExpanded As New List(Of Nullable(Of Boolean))
        Dim listChecked As New List(Of Nullable(Of Boolean))
        If HttpContext.Current.Session("1" + key + treeView.ID) IsNot Nothing Then
            listExpanded = CType(HttpContext.Current.Session("1" + key + treeView.ID), List(Of Nullable(Of Boolean)))
            listChecked = CType(HttpContext.Current.Session("2" + key + treeView.ID), List(Of Nullable(Of Boolean)))
       End If
 
        RestoreTreeViewIndex = 0
        RestoreTreeViewExpandedState(treeView.Nodes, listExpanded)
        RestoreTreeViewIndex = 0
        RestoreTreeViewCheckedState(treeView.Nodes, listChecked)
    End Sub
 
    Private Sub RestoreTreeViewExpandedState(ByVal nodes As TreeNodeCollection, ByVal list As List(Of Nullable(Of Boolean)))
        For Each node As TreeNode In nodes
            If RestoreTreeViewIndex >= list.Count Then Exit Sub
 
            node.Expanded = list(RestoreTreeViewIndex)
            RestoreTreeViewIndex += 1
 
            If node.ChildNodes.Count > 0 Then
                RestoreTreeViewExpandedState(node.ChildNodes, list)
            End If
        Next
    End Sub
    Private Sub RestoreTreeViewCheckedState(ByVal nodes As TreeNodeCollection, ByVal list As List(Of Nullable(Of Boolean)))
        For Each node As TreeNode In nodes
            If RestoreTreeViewIndex >= list.Count Then Exit Sub
 
            node.Checked = list(RestoreTreeViewIndex)
            'If node.Checked Then
            '    System.Diagnostics.Debug.Write("X")
            'End If
            RestoreTreeViewIndex += 1
 
            If node.ChildNodes.Count > 0 Then
                RestoreTreeViewCheckedState(node.ChildNodes, list)
            End If
        Next
    End Sub
End Class
 

 

Posted on Friday, October 12, 2007 1:44 AM TreeView | Back to top


Comments on this post: Save TreeView Nodes Expansion/Collapse & Checkboxes State In Session

# re: Save TreeView Nodes Expansion/Collapse & Checkboxes State In Session
Requesting Gravatar...
Excellent work.

Any chance you could provide a working example for a treeview on a windows form? (i.e. a local application, not http)

Many thanks!
Left by Eric on Jul 06, 2008 12:33 PM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net