65.9K
CodeProject is changing. Read more.
Home

Save and Load TreeView content

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.57/5 (26 votes)

Mar 31, 2003

1 min read

viewsIcon

163661

downloadIcon

4289

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!