Solved

Save and Load TreeNodeCollection

Posted on 2011-09-20
3
878 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now