Solved

Save and Load TreeNodeCollection

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

752 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