Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / C#
Article

Save and Load TreeView content

Rate me:
Please Sign up or sign in to vote.
3.57/5 (26 votes)
13 Jun 20051 min read 161.5K   4.3K   37   28
A class that implements routines for saving and loading TreeView content using serialization.

Introduction

This is a class named LoadAndSave in the jhTreeViewTools namespace. I wrote it, because I needed to save and load the content of a TreeView in correct order including the Tags of each node. And I wanted to use serialization for that.

This is why I strongly required that there is nothing stored in the Node.Tag that isn't serializable because this will cause an error message. I am serializing the whole root-nodes, so everything that is stored in that Node and its children will be saved. I am still wondering why the ToolTip-text is not saved although it's also part of a Node!?

Details

There are only two public methods:

  • public static int saveTree(TreeView tree, string filename)
  • public static int loadTree(TreeView tree, string filename)

With those you can save and load the contents only by giving the TreeView control and the file name as arguments.

I use this class in my application in mostly the same way but I changed the main-descriptions of each method to the English language. The rest of the code-comments are in German because I haven't got the time to translate everything. Please try it and if there is anything that I may change or if you find mistakes in my code, please email me. Remind yourself that I am a C# beginner!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Database Developer
Germany Germany
I am programming more or less since I was 15 years.
It started with BASIC and during school I did
a lot Pascal programs for MS DOS.
After that I came to visual Windows programming and coded a lot in Delphi and later in C# .NET.
I made also some excursions in C and C++.

I also like Perl, Python and other scripting languages.
After a long break not programming anything, the last approx. 5-6 years I am only working with Java and SQL.

New for me is Objective-C - I am starting to get involved into Mac Programming Smile | :)

Comments and Discussions

 
QuestionThank you. Pin
Member 1473881226-Jun-20 16:12
Member 1473881226-Jun-20 16:12 
PraiseThank you Pin
cyberdive12-Jan-20 6:10
cyberdive12-Jan-20 6:10 
Questionhelp Pin
nkverma200728-Mar-17 19:58
nkverma200728-Mar-17 19:58 
GeneralMy vote of 5 Pin
Gun Gun Febrianza14-Nov-14 22:30
Gun Gun Febrianza14-Nov-14 22:30 
QuestionThanks! Pin
Member 1088107512-Jun-14 8:47
Member 1088107512-Jun-14 8:47 
QuestionGreat code Pin
countrysideflair19-Jul-11 9:26
countrysideflair19-Jul-11 9:26 
GeneralCan't save stateimageindex using u'r code Pin
mr.nothing6-May-11 5:32
mr.nothing6-May-11 5:32 
Hello!
First of all thanks a lot for this code, but actually, using this I found out that I can save stateimageindex using this code. Could you please help me with this or just give me some pieces of advice?
AnswerRe: Can't save stateimageindex using u'r code Pin
j-hannemann9-May-11 4:52
j-hannemann9-May-11 4:52 
GeneralSaving to registry using Binary Stream Pin
msanjay7530-Jul-09 4:53
msanjay7530-Jul-09 4:53 
GeneralGreat code! Pin
luk1413316-Feb-09 0:05
luk1413316-Feb-09 0:05 
Generalproblem while saving Pin
reji.sukesan10-Jul-07 14:59
reji.sukesan10-Jul-07 14:59 
Questionhow to implemt in project Pin
ikkevd7-Apr-07 3:47
ikkevd7-Apr-07 3:47 
AnswerRe: how to implemt in project Pin
j-hannemann14-May-07 19:14
j-hannemann14-May-07 19:14 
GeneralThank You Pin
Andy Rama27-Nov-06 3:22
Andy Rama27-Nov-06 3:22 
QuestionRe: Thank You ( 'DataGridViewRow is not marked as Serializable') [modified] Pin
Andy Rama28-Nov-06 2:30
Andy Rama28-Nov-06 2:30 
GeneralNice One!! Pin
J101uk24-Sep-06 10:10
J101uk24-Sep-06 10:10 
GeneralChildnode Problem Pin
David Rider5-Feb-06 6:37
David Rider5-Feb-06 6:37 
GeneralGood Job Pin
PHDENG8124-Jan-06 4:31
PHDENG8124-Jan-06 4:31 
GeneralBUG!!! Pin
lior654#hotmail.com12-May-05 7:16
lior654#hotmail.com12-May-05 7:16 
GeneralRe: BUG!!! Pin
j-hannemann14-Jun-05 7:51
j-hannemann14-Jun-05 7:51 
GeneralA couple suggestions... Pin
Stephane Rodriguez.30-Mar-03 23:28
Stephane Rodriguez.30-Mar-03 23:28 
GeneralRe: A couple suggestions... Pin
j-hannemann31-Mar-03 1:10
j-hannemann31-Mar-03 1:10 
GeneralRe: A couple suggestions... Pin
Stephane Rodriguez.31-Mar-03 1:40
Stephane Rodriguez.31-Mar-03 1:40 
GeneralRe: A couple suggestions... Pin
j-hannemann31-Mar-03 3:03
j-hannemann31-Mar-03 3:03 
GeneralRe: A couple suggestions... Pin
31-Mar-03 3:14
suss31-Mar-03 3:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.