473,756 Members | 6,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Save and Load TreeView to a File in VB.NET

Ensonix
2 New Member
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 2286

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

Similar topics

4
25029
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 less complex technical calculations, several input variables. I would like to equipp the starting panel with the usual New, Open, Save, Save As, Close etc. menus (like in Excel, or Word, etc.) What is the best way to accomplish Save, or Save As?...
1
6165
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 the state of the expanded treeview so that I can apply the same expanded state to the updated treeview. is there something i can do?
4
3718
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 file via some editable controls such as text boxes , option boxes etc. how can i implment this , should i use another xslt file with <INPUT> controls . if so how can i save the result back using the asp.net
14
15097
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
18679
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 them from the Table back into the TreeView when the app starts up. (If the table is empty, then just exit the sub)
1
6825
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 file during specific events in the script execution? image format doesnt matter. thanks! christine
1
1742
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 name from the list. How can I do that? If I could open the file even with out the dialog box, that would work. For example, when user clicks on a PDF file, then it should open in PDF reader. Any help is appreciated.
3
4442
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 reading back that XML file will rebuild the same treenode structure. Does anyone have any idea or done such thinkg ?
2
2239
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 representing a message. I noticed that the TreeView loads alright from the XML file but did not save anything. I call the TreeView.Save() for each new node added into the TreeView. What I'm missing here? Thanks in advance!
0
9454
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9868
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6533
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5139
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2664
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.