473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Save and Load TreeView to a File in VB.NET

Ensonix
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
0 2233

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

Similar topics

4
by: Andras Gilicz | last post by:
Hi VB fans I'm working on a relatively large project in VB6 with about a dozen forms, including graphs, labels, text boxes, etc. The software itself is actually a flow simulator with more or...
1
by: R. Sammut | last post by:
Hi, I have a treeview control that is updated every 30 seconds. The treeview nodes are cleared and new nodes (same number of nodes and hierarchy) are added to the treeview. Now I need to save...
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
14
by: Mr.D | last post by:
How do I save/load the contents of a Treeview to a file? I have found several good examples written i VB6, but not a single one for VB.NET. Please help. ---- Tim
1
by: cjinsocal581 | last post by:
Hi all, I am struggling with the following: Environement: VB.NET 2005 TreeView Control SQL Database I need to be able to save a TreeView's nodes into a SQL database and then be able to call...
1
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image...
1
by: shil | last post by:
Hi, I am trying to list some files from server using .net 2005 treeview control, which I could successfully do it. Also, I would like to open the Open/Save dialog box when user clicks on a file...
3
by: serge calderara | last post by:
Dear all, I have a treeView control which is build with different parent and childs node objects. I try to find out an easy and proper way to save that nodes architecture in a XML file. Then...
2
by: Polaris | last post by:
Hi Experts: I'm using ASP.NET 2.0. I'm using a TreeView control with a design-time configured XmlDataSource (an XML file). I use the TreeView control as a "message board" with each node...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.