views:

114

answers:

1

Hello, basically my project is an MDI Winform application where a user can customize the interface by adding various controls and changing the layout. I would like to be able to save the state of the application for each user.

I have done quite a bit of searching and found these:

http://stackoverflow.com/questions/2076259/how-to-auto-save-and-auto-load-all-properties-in-winforms-c

http://stackoverflow.com/questions/1669522/c-save-winform-or-controls-to-file

Basically from what I understand, the best approach is to serialize the data to XML, however winform controls are not serializable, so I would have use surrogate classes:

http://www.codeproject.com/KB/dotnet/Surrogate_Serialization.aspx

Now, do I need to write a surrogate class for each of my controls? I would need to write some sort of a recursive algorithm to save all my controls, what is the best approach to do accomplish that? How would I then restore all the windows, should I use the memento design pattern for that? If I want to implement multiple users later, should I use Nhibernate to store all the object data in a database? I am still trying to wrap my head around the problem and if anyone has any experience or advice I would greatly appreciate it, thanks

+1  A: 

You don't want to serialize the actual control instances. They should be created and destroyed along with the Form they reside in. Rather look at what you let the user customize. Layout and position? Very well, save out the Top and Left coordinates for each control along with a control identifier. Do you let the user add new controls? Save their ids along with a type identifier so when its time to reload you are able to recreate the controls at their previous position.

Whether you use XML or some other format, there is no best approach or best practice, choose what makes sense for your project. XML happens to be an easy to go with format with great support in the .Net Framework.

Peter Lillevold
That's kind of what I was thinking as well, I'm just trying to figure out how to best implement that. Do I need to create an object that would store these properties that I would then serialize? What is the best way to iterate through the form in order to capture all the controls and their properties?
Serge
Iterate over Form.Controls and write out their properties as you iterate. Restoring state is similar but requires a bit more work if you support adding new controls.
Peter Lillevold