Click here to Skip to main content
15,880,469 members
Articles / Desktop Programming / Windows Forms

Using Serialization to Persist TreeView Control (VB.NET)

Rate me:
Please Sign up or sign in to vote.
4.81/5 (40 votes)
27 Oct 2004CPOL5 min read 322.7K   4.5K   87  
Utilising serialization to persist TreeView control hierarchy.
Option Strict On

''' <summary>
''' The TreeViewDataAccess class allows the nodes within a TreeView to be 
''' persisted to xml for later retrevial.
''' </summary>
Public Class TreeViewDataAccess

#Region "Structures"

    ''' <summary>
    ''' TreeViewData structure represents the root node collection of a TreeView
    ''' and provides the PopulateTreeView function to add these nodes to a specified
    ''' TreeView instance.
    ''' </summary>
    <Serializable()> Public Structure TreeViewData

        ''' <summary>Array of TreeNodeData objects representing the root nodes in a TreeView.</summary>
        Public Nodes() As TreeNodeData

        ''' <summary>
        ''' Creates new instance of the TreeViewData structure based from the 
        ''' specified TreeView.
        ''' </summary>
        ''' <param name="treeview">TreeView to build the TreeViewData instance from.</param>
        Public Sub New(ByVal treeview As TreeView)

            'Check to see if there are any root nodes in the TreeView
            If treeview.Nodes.Count = 0 Then Exit Sub

            'Populate the Nodes array with child nodes
            ReDim Nodes(treeview.Nodes.Count - 1)
            For i As Integer = 0 To treeview.Nodes.Count - 1
                Nodes(i) = New TreeNodeData(treeview.Nodes(i))
            Next
        End Sub

        ''' <summary>
        ''' Populates the specified TreeView with the current TreeViewData instance.
        ''' </summary>
        ''' <param name="treeview">TreeView instance to populate.</param>
        Public Sub PopulateTree(ByVal treeview As TreeView)
            'Check to see if there are any root nodes in the TreeViewData
            If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit Sub

            'Populate the TreeView with child nodes
            treeview.BeginUpdate()
            For i As Integer = 0 To Me.Nodes.Length - 1
                treeview.Nodes.Add(Me.Nodes(i).ToTreeNode)
            Next
            treeview.EndUpdate()
        End Sub

    End Structure

    ''' <summary>
    ''' TreeNodeData structure represents a TreeNode and provides the
    ''' ToTreeNode function to convert the instance to a TreeNode object.
    ''' </summary>
    <Serializable()> Public Structure TreeNodeData

        ''' <summary>String representing the Text property of the TreeNode.</summary>
        Public Text As String
        ''' <summary>Integer representing the ImageIndex property of the TreeNode.</summary>
        Public ImageIndex As Integer
        ''' <summary>Integer representing the SelectedImageIndex property of the TreeNode.</summary>
        Public SelectedImageIndex As Integer
        ''' <summary>Boolean representing the Checked state of the TreeNode.</summary>
        Public Checked As Boolean
        ''' <summary>Boolean representing the Expanded state of the TreeNode.</summary>
        Public Expanded As Boolean
        ''' <summary>Object representing the Tag property of the TreeNode.</summary>
        Public Tag As Object
        ''' <summary>Array of TreeNodeData objects representing the root nodes in a TreeView.</summary>
        Public Nodes() As TreeNodeData

        ''' <summary>
        ''' Creates new instance of the TreeNodeData structure based on the specified TreeNode.
        ''' </summary>
        ''' <param name="node">TreeNode to build the TreeNodeData instance from.</param>
        Public Sub New(ByVal node As TreeNode)
            'Set the basic TreeNode properties
            Me.Text = node.Text
            Me.ImageIndex = node.ImageIndex
            Me.SelectedImageIndex = node.SelectedImageIndex
            Me.Checked = node.Checked
            Me.Expanded = node.IsExpanded

            'See if there is an object in the tag property and if it is serializable
            If (Not node.Tag Is Nothing) AndAlso node.Tag.GetType.IsSerializable Then Me.Tag = node.Tag

            'Check to see if there are any child nodes
            If node.Nodes.Count = 0 Then Exit Sub

            'Recurse through child nodes and add to Nodes array
            ReDim Nodes(node.Nodes.Count - 1)
            For i As Integer = 0 To node.Nodes.Count - 1
                Nodes(i) = New TreeNodeData(node.Nodes(i))
            Next
        End Sub

        ''' <summary>
        ''' Returns as TreeNode built from the instance of the TreeNodeData object.
        ''' </summary>
        Public Function ToTreeNode() As TreeNode
            'Create TreeNode based on instance of TreeNodeData and set basic properties
            ToTreeNode = New TreeNode(Me.Text, Me.ImageIndex, Me.SelectedImageIndex)
            ToTreeNode.Checked = Me.Checked
            ToTreeNode.Tag = Me.Tag
            If Me.Expanded Then ToTreeNode.Expand()

            'Recurse through child nodes adding to Nodes collection
            If Me.Nodes Is Nothing OrElse Me.Nodes.Length = 0 Then Exit Function
            For i As Integer = 0 To Me.Nodes.Length - 1
                ToTreeNode.Nodes.Add(Me.Nodes(i).ToTreeNode)
            Next
        End Function

    End Structure

#End Region

#Region "Public"

    ''' <summary>
    ''' Populates the specified TreeView from the serialized TreeViewData structure file specified.
    ''' </summary>
    ''' <param name="treeView">TreeView instance to populate.</param>
    ''' <param name="path">Serialized Xml representation of TreeViewData</param>
    Public Shared Sub LoadTreeViewData(ByVal treeView As TreeView, ByVal path As String)
        'Create as serializer and get the file to deserialize
        Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
        Dim file As New System.IO.FileStream(path, IO.FileMode.Open)
        Dim reader As New System.Xml.XmlTextReader(file)

        'Deserialize the file and populate the treeview
        Dim treeData As TreeViewData = CType(ser.Deserialize(reader), TreeViewData)
        treeData.PopulateTree(treeView)

        'Tidy up
        reader.Close()
        file.Close()
        file = Nothing
    End Sub

    ''' <summary>
    ''' Saves the specified TreeView in serialized TreeViewData structure file specified.
    ''' </summary>
    ''' <param name="treeView">TreeView instance to save.</param>
    ''' <param name="path">Path to store serialized file.</param>
    Public Shared Sub SaveTreeViewData(ByVal treeView As TreeView, ByVal path As String)
        'Create as serializer and file to save TreeViewData
        Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(TreeViewData))
        Dim file As New System.IO.FileStream(path, IO.FileMode.Create)
        Dim writer As New System.Xml.XmlTextWriter(file, Nothing)

        'Generate TreeViewData from TreeView and serialize the file.
        ser.Serialize(writer, New TreeViewData(treeView))

        'Tidy up
        writer.Close()
        file.Close()
        file = Nothing
    End Sub

#End Region

End Class



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) RedFrog.com Limited
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions