Search KB Filter article types
TreeView CodeSample - Serializing and Deserializing a TreeView
Categories: Trees, State Serialization
Tags: Developers, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
Revision: 1
Posted: 07/Oct/2009
Updated: 23/July/2010
Status: Publish
Types: Article

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample
Overview

This codesample will demonstrate how you can serialize a subset of the properties of nodes in a TreeView and then deserialize and load into another TreeView.

A custom serializable class is declared that will contain the subset of the properties desired for serialization. Those properties can easily be expanded at will.

How the app works

This demo app is a Form with a left and a right TreeView. On load, the left TreeView is populated with 10 nodes with 5 subnodes each. On the click of the button, the Text and IsExpanded properties of the nodes are serialized and then deserialized and loaded into the TreeView on right. Try shrinking / expanding the nodes on left and then do the copy.

Copy can be performed as often as you like.

VB.NET Code
#Region "Imports"
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports Gizmox.WebGUI.Forms
#End Region
Public Class SerializingTreeView
 
#Region "Constructor and controls"
    Friend WithEvents TreeView1 As Gizmox.WebGUI.Forms.TreeView
    Friend WithEvents TreeView2 As Gizmox.WebGUI.Forms.TreeView
    Friend WithEvents Button1 As Gizmox.WebGUI.Forms.Button
 
    Sub New()
        MyBase.New()
 
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        Me.TreeView1 = New Gizmox.WebGUI.Forms.TreeView
        Me.TreeView2 = New Gizmox.WebGUI.Forms.TreeView
        Me.Button1 = New Gizmox.WebGUI.Forms.Button
        '
        'TreeView1
        '
        Me.TreeView1.Location = New System.Drawing.Point(12, 59)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.Size = New System.Drawing.Size(226, 177)
        Me.TreeView1.TabIndex = 0
        '
        'TreeView2
        '
        Me.TreeView2.Location = New System.Drawing.Point(261, 59)
        Me.TreeView2.Name = "TreeView2"
        Me.TreeView2.Size = New System.Drawing.Size(232, 177)
        Me.TreeView2.TabIndex = 1
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(261, 13)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(120, 23)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Copy TreeView"
 
        '
        'SerializingTreeView
        '
        Me.Size = New System.Drawing.Size(801, 457)
        Me.Text = "SerializingTreeView"
 
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TreeView2)
        Me.Controls.Add(Me.TreeView1)
    End Sub
#End Region
 
#Region "Custom class"
    <Serializable()> _
    Public Class CustomNode
        Public ChildNodes() As CustomNode
        Public Text As String
        Public IsExpanded As Boolean
    End Class
 
    Public Function CustomizeTreeView(ByVal T As TreeView) As CustomNode()
        Dim CNarray(T.Nodes.Count - 1) As CustomNode
        For i = 0 To T.Nodes.Count - 1
            Dim TN As TreeNode = T.Nodes(i)
            Dim CN As CustomNode = New CustomNode()
            CN.Text = TN.Text
            CN.IsExpanded = TN.IsExpanded
            If TN.HasNodes Then
                CN.ChildNodes = CustomizeTreeView(TN)
            End If
            CNarray(i) = CN
        Next
        Return CNarray
    End Function
#End Region
 
#Region "Custom helpers"
    Public Function CustomizeTreeView(ByVal TopNode As TreeNode) As CustomNode()
        Dim CNarray(TopNode.Nodes.Count - 1) As CustomNode
        For i = 0 To TopNode.Nodes.Count - 1
            Dim TN As TreeNode = TopNode.Nodes(i)
            Dim CN As CustomNode = New CustomNode()
            CN.Text = TN.Text
            CN.IsExpanded = TN.IsExpanded
            If TN.HasNodes Then
                CN.ChildNodes = CustomizeTreeView(TN)
            End If
            CNarray(i) = CN
        Next
        Return CNarray
    End Function
 
    Public Sub LoadCustomizedTreeView(ByVal T As TreeView, ByVal CNarray() As CustomNode)
        T.Nodes.Clear()
        For Each CN As CustomNode In CNarray
            Dim TN As TreeNode = New TreeNode(CN.Text)
            If CN.ChildNodes IsNot Nothing Then
                LoadCustomizedTreeView(TN, CN.ChildNodes)
            End If
            TN.IsExpanded = CN.IsExpanded
            T.Nodes.Add(TN)
        Next
    End Sub
    Public Sub LoadCustomizedTreeView(ByVal topNode As TreeNode, ByVal CNarray() As CustomNode)
        For Each CN As CustomNode In CNarray
            Dim TN As TreeNode = New TreeNode(CN.Text)
            If CN.ChildNodes IsNot Nothing Then
                LoadCustomizedTreeView(TN, CN.ChildNodes)
            End If
            TN.IsExpanded = CN.IsExpanded
            topNode.Nodes.Add(TN)
        Next
    End Sub
#End Region
 
#Region "Serializing helpers"
    Public Sub SerializeToMemory(Of T)(ByVal memStream As MemoryStream, ByVal obj As T)
        ' Create a formatter for this file stream.
        Dim bf As New BinaryFormatter(Nothing, _
           New StreamingContext(StreamingContextStates.Other))
        ' Serialize the object and close the stream.
        bf.Serialize(memStream, obj)
    End Sub
 
    Public Function SerializeToMemory(Of T)(ByVal obj As T) As MemoryStream
        Dim MS As MemoryStream = New MemoryStream
        SerializeToMemory(MS, obj)
        Return MS
    End Function
 
    Public Function DeserializeFromMemory(Of T)(ByVal memStream As MemoryStream) As T
        ' Create a formatter for this file stream.
        Dim bf As New BinaryFormatter(Nothing, _
           New StreamingContext(StreamingContextStates.Other))
        ' Deserialize the object from the stream.
        Return DirectCast(bf.Deserialize(memStream), T)
    End Function
#End Region
 
#Region "Event handlers"
    Private Sub SerializingTreeView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 10
            Dim TopNode As TreeNode = New TreeNode("TopNode " + i.ToString)
            TreeView1.Nodes.Add(TopNode)
            For j As Integer = 1 To 5
                Dim ChildNode As TreeNode = New TreeNode("Child " + i.ToString + "-" + j.ToString)
                TopNode.Nodes.Add(ChildNode)
            Next
        Next
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim CNarray() As CustomNode = CustomizeTreeView(TreeView1)
        Dim MS As MemoryStream = New MemoryStream
        SerializeToMemory(MS, CNarray)
        MS.Seek(0, SeekOrigin.Begin)
        Dim NewCNarray() As CustomNode = DeserializeFromMemory(Of CustomNode())(MS)
        LoadCustomizedTreeView(TreeView2, NewCNarray)
    End Sub
#End Region
End Class

C# Code
The conversion of this code to C# has not been completed yet.

About the author

Related Articles

Trees  
Title Update Author
Control of a combobox with a checked list and a tree.
Tags: Developers, Navigation, 2. Intermediate, 3. Advanced, Customization, Layouting, Navigation, Pre v6.3, v6.3, v6.4 and Later
06/Jan/2008    2008/01/06
This video demonstrates how to populate a TreeView control.
Tags: Developers, Data Binding, 1. Beginner, Data Binding, Pre v6.3, v6.3, v6.4 and Later, 2. Intermediate, 3. Advanced
01/Jan/2009    2009/01/01
This code snippet posted By NEC_VERSA demonstrates how to find a node in a tree view.
Tags: Developers, 1. Beginner, Pre v6.3, v6.3, v6.4 and Later, 2. Intermediate, 3. Advanced
12/Jan/2008    2008/01/12
This article contains code that demonstrates how to create a personalized look for TreeNodes that have the mouse hovering over them while dragging another element.
Tags: Developers, Drag & Drop, Events, C#, 2. Intermediate, 3. Advanced, Customization, v6.3, v6.4 and Later
06/Jan/2010    2010/01/06
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Developers, Drag & Drop, 1. Beginner, Pre v6.3, v6.3, v6.4 and Later, 2. Intermediate, 3. Advanced
23/July/2010    2010/07/23
.NET HTML5 Web, Cloud and Mobile application delivery | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2012 Visual WebGui®       Visual WebGui weblog on ASP.NET Gizmox Blog Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS