Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Save and Load TreeNodeCollection

Posted on 2011-09-20
3
Medium Priority
?
892 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

886 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