Solved

Save and Load TreeNodeCollection

Posted on 2011-09-20
3
889 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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