By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Save and Load TreeView to a File in VB.NET

Ensonix
P: 2
I found a post about this, and it had some C# code that wouldn't convert, and when I finally got it converted it didn't work in .NET 1.1. So I made a few changes to the converted VB version and now it works perfect, however I could find a post replay on that form, so I thought I'd put it here for anyone else searching.

Expand|Select|Wrap|Line Numbers
  1. 'ENSONIX ENTERPRISES, INCORPORATED
  2. 'WEB:            http://www.ensonix.com
  3. '============================================================
  4. 'CONVERTED FROM: Original C# jhTreeViewTools
  5. 'CREATED BY:     j-hannemann
  6. 'WEB:            http://www.jhmedia.de
  7. '------------------------------------------------------------
  8. 'CONVERTED BY:   Jason Martin of EEI
  9. 'CONVERTED ON:   January 1st, 2008
  10. 'CODE VERSION:   VB.NET 2003, .NET 1.1
  11.  
  12. #REGION " >> IMPORTS                                       "
  13.  
  14.   Imports System
  15.   Imports System.Collections
  16.   Imports System.Windows.Forms
  17.   Imports System.IO
  18.   Imports System.Runtime.Serialization.Formatters.Binary
  19.  
  20. #END REGION
  21.  
  22. NameSpace Utilities
  23. Public Class Tree
  24.  
  25.   #REGION " >> ENUMERATORS                                   "
  26.  
  27.     Public Enum ErrorCodeTypes As Integer
  28.       None          =  0
  29.       Serialization = -1
  30.       File          = -2
  31.       Exception     = -3
  32.     End Enum
  33.  
  34.   #END REGION
  35.  
  36.   #REGION " >> SHARED METHODS                                "
  37.  
  38.         Public Shared Function Save(ByVal TreeViewToSave As TreeView, ByVal FileName As String) As Tree.ErrorCodeTypes
  39.  
  40.       Dim errorCode   As Tree.ErrorCodeTypes  = ErrorCodeTypes.None
  41.             Dim rootNodes   As ArrayList            = Nothing
  42.       Dim fileStream  As FileStream           = Nothing
  43.             Dim formatter   As BinaryFormatter      = Nothing
  44.  
  45.       Try
  46.  
  47.         rootNodes   = New ArrayList()
  48.         fileStream  = New FileStream(FileName,FileMode.Create)
  49.         formatter   = New BinaryFormatter()
  50.  
  51.               For Each node As TreeNode In TreeViewToSave.Nodes
  52.                   rootNodes.Add(node)
  53.               Next
  54.  
  55.               Try
  56.           formatter.Serialize(fileStream, rootNodes)
  57.               Catch ex As System.Runtime.Serialization.SerializationException
  58.                   errorCode = ErrorCodeTypes.Serialization
  59.                   Throw(ex)
  60.               End Try
  61.  
  62.       Catch ex As Exception
  63.         errorCode = ErrorCodeTypes.Exception
  64.         Throw(ex)
  65.       Finally
  66.         If Not(fileStream Is Nothing) Then fileStream.Close()
  67.       End Try
  68.  
  69.              Return errorCode
  70.  
  71.         End Function
  72.  
  73.         Public Shared Function Load(ByVal TreeViewToLoad As TreeView, ByVal FileName As String) As Tree.ErrorCodeTypes
  74.  
  75.       Dim errorCode   As Tree.ErrorCodeTypes  = ErrorCodeTypes.None              
  76.       Dim nodeList    As ArrayList            = Nothing
  77.       Dim fileStream  As FileStream           = Nothing
  78.       Dim formatter   As BinaryFormatter      = Nothing
  79.       Dim rootNodes   As Object               = Nothing
  80.  
  81.       Try
  82.  
  83.         If File.Exists(FileName) Then
  84.  
  85.           fileStream  = New FileStream(FileName,FileMode.Open)
  86.                   formatter   = New BinaryFormatter() 
  87.  
  88.                   Try
  89.                       rootNodes = formatter.Deserialize(fileStream)
  90.                   Catch ex As System.Runtime.Serialization.SerializationException
  91.                       errorCode = ErrorCodeTypes.Serialization
  92.             Throw(ex)
  93.                   End Try
  94.  
  95.           For Each node As TreeNode In CType(rootNodes, ArrayList)
  96.             TreeViewToLoad.Nodes.Add(node) 
  97.           Next 
  98.  
  99.         Else 
  100.           errorCode = ErrorCodeTypes.File
  101.         End If 
  102.  
  103.       Catch ex As Exception
  104.         errorCode = ErrorCodeTypes.Exception
  105.         Throw(ex)
  106.       Finally
  107.         If Not(fileStream Is Nothing) Then fileStream.Close()
  108.       End Try
  109.  
  110.       Return errorCode        
  111.  
  112.     End Function
  113.  
  114.   #END REGION
  115.  
  116. End Class
  117. End NameSpace
Also make sure that if you are including a tag (ie TreeNode.Tag) and your Tag Object is you own class that you make it Serializable for that information to save also.

EXAMPLE
Expand|Select|Wrap|Line Numbers
  1. <Serializable()> _
  2. Public Class Person
  3.   Public NameFirst As String
  4.   Public NameLast As String
  5. End Class
Thanks,
Jason Martin
Jan 2 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.