Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a treeview with checkboxes. I'm looking for some pointers or suggestions on how to persist the checkboxes in the tree so when I restart my app, I can recheck the same checkboxes. I'm looking for suggestions as to where I should be looking on how to do this? Will a List work for this? Whats the best structure/scheme to do this?

Thanks...
Posted
Updated 24-Sep-13 14:23pm
v2
Comments
BillWoodruff 25-Sep-13 1:40am    
Are you doing something now that persists the TreeNodes, or are you loading the TreeView using DataBinding from some source ? Is this Windows Forms ?
rfresh 25-Sep-13 12:05pm    
I'm not doing any persisting now...I'm looking for a way to persist the checkboxes and node names of a treeview so that when I restart my app, the tree checkboxes will be checked again. I'm doing a winform app. I'll also look into DataBinding Bill. Thanks...

1 solution

You need to separate UI from data and other aspects of the application. Create some data model of the information edited in your UI. It could be just a set of some pure-data classes/structures. You UI should be aware of this model, but model unaware of the UI.

Now, develop, in the simplest case 1) initialization of the data instance of model (for a minute, forget persistence), 2) population of the UI from data, 3) update of the data from controls edited using this UI.

When this is done, you have all but persistence. For persistence, use Data Contract: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This approach is the most non-intrusive, robust end easy to use. You won't need to modify your classes, will need just to add some attributes. You won't need to work with files and XML or JSON directly. The data contract serializer will simply store any arbitrary object graph according to contract (if does not even have to be a tree, could be anything, even with circular references) and restore it later as it was, from/to any stream of a file. Performance is also very good, based on generation assemblies on the fly with System.Reflection.Emit.

Please see my past answers advocating this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA
 
Share this answer
 
Comments
[no name] 24-Sep-13 21:18pm    
A complete answer.
Sergey Alexandrovich Kryukov 24-Sep-13 21:25pm    
Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900