I’m trying to implement a Load / Save function for a Windows Forms application.
I’ve got following components:
- A tree view
- A couple of list views
- A couple of text boxes
- A couple of objects (which holds a big dictionarylist)
I want to implement a way to save all of this into a file, and resume/load it later on.
What’s the best way to do this?
I think XML serialization is the way to go, but I’m not quite sure how, or where to start. Or will it require a really complex solution to be able to do this?
Here’s an example that binds an object and some ancestors to the UI; the use of C# 3.0 here is purely for brevity – everything would work with C# 2.0 too.
Most of the code here is setting up the form, and/or dealing with property-change notifications – importantly, there isn’t any code devoted to updating the UI from the object model, or the object model from the UI.
Note also the IDE can do a lot of the data-binding code for you, simply by dropping a BindingSource onto the form and setting the DataSource to a type via the dialog in the property grid.
Note that it isn’t essential to provide property change notifications (the PropertyChanged stuff) – however, most 2-way UI binding will work considerably better if you do implement this. Not that PostSharp has some interesting ways of doing this with minimal code.