Solved

Save and Load TreeNodeCollection

Posted on 2011-09-20
3
884 Views
Last Modified: 2012-05-12
Hello,

I have an older Windows app (.NET 2.0) that I need to modify as minimally as possible.  It contains a treeview that has several rather extensive TreeNodeCollections that load into it depending on the users choices.  This takes awhile to load, because it is pulling data from the database to create the nodes.

One of the requests I have is (after the tree first loads) to save the current TreeNodeCollection to memory for later use.  That way, if the user loads a different tree, and then comes back to the first, it could (in theory) load much faster because it won't have to run all the queries to the database a second time.

So:
1) Application Starts
2) User loads Tree 1 (Tree 1 is loaded from the database)
3) User loads Tree 2 (Tree 1 is saved to memory and Tree 2 is loaded from the database)
4) User loads Tree 1 (Tree 1 is loaded from memory)
5) User loads Tree 2 (Tree 2 is loaded from memory)

I don't want to re-invent the wheel here, so has someone done this before and/or have a link to an article somewhere?

Thanks,
-Torrwin
0
Comment
Question by:Torrwin
3 Comments
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36572024
On button1 I fill TreeView

Button2 - Saves Treeview nodes   To  a TreeNode() var   and also  serializes to memorystream an saves it to  Byte Array.

Button3 -  Clears Treeview Nodes and Resotre from TreeNode array.

Button4  - Clears Treeview Nodes and Resotre from byte array.

Imports System.IO
Imports System.Data
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       For Each s In "a,b,c,d,e".Split(",")
           Dim nd = Me.Tv.Nodes.Add(s)
               For Each s1 In "1,2,3,4,5".Split(","c)
                   Dim x = nd.Nodes.Add(s1)
               Next
       Next
End Sub
Private savenodes As TreeNode()
Dim bb As Byte()
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
     savenodes = New TreeNode(Tv.Nodes.Count - 1) {}
     Tv.Nodes.CopyTo(savenodes, 0)
     Tv.Nodes.Clear()
     Dim ms As New MemoryStream(4000)
     Dim Formatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
     Formatter.Serialize(ms, savenodes)
     bb = ms.ToArray
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Tv.Nodes.Clear()
    If savenodes IsNot Nothing Then Tv.Nodes.AddRange(savenodes)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Tv.Nodes.Clear()
    If bb IsNot Nothing Then
       Dim Formatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
       Tv.Nodes.AddRange(DirectCast(Formatter.Deserialize(New MemoryStream(bb)), TreeNode()))
    End If
End Sub
End Class

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36576325
Are all the nodes on same level? If not, you may want to try loading on demand using BeforeExpand event of the node to child nodes only if the parent node is expanded.
0
 
LVL 13

Author Closing Comment

by:Torrwin
ID: 36595881
We're still in testing, but this seems to have done the trick.  Thanks!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question