Saving and restoring a form's state is an important requirement in many applications (e.g. to remember application settings). If a form hosts many controls this can result in huge amounts of code. This code needs to be maintained if the form changes. It's obvious that a general approach would ease this task.
If you try to serialize a windows form object, you will notice that in the .NET world these classes don't support serialization per se. This makes sense, since in a MVC designed architecture, it's the model's data you'd want to serialize, not the control itself.
However, many controls are fairly simple, like a checkbox, radiobutton or a slidercontrol. Their behaviour and appearance can be described by a number of properties (e.g. size, visibility state or the selection index of a combo box). These properties have no complex data structures and can easily be serialized to save the form's state. But the question is, which properties to choose and how to handle them... and what about saving as much as possible?
UIPSnap (User Interface Property Snap) is a library for serializing the state of windows forms and controls recursively to XML by reading and writing properties of certain types on a best-effort basis using reflection. It's very simple to use and yet provides some interesting possibilities of customizing.
Take a look at this example code:
Putting these 3 lines into a form's closing event handler will do the trick and save the form's state into an XML file. Similar code is needed to restore the form (e.g. in the form's shown event handler):
Here is a screenshot of an example GUI whose state can be handled by UIPSnap:
So, which properties are actually saved? There are some rules:
There are some special cases that need to be handled properly in case of certain control types (e.g. UIPSnap handles also item collections of list and comboboxes and saves and restores multiple selection indices).
The above code is part of a visual studio example project bundled with the UIPSnap library binaries.
To download now, use this link. The software is shareware, but fully functional.
How to obtain a license key
UIPSnap is shareware. You may test it for a duration of 30 days. After that you need to register to get a registration key against a small fee of 3,49 EUR (including VAT). Registered users are granted permission to bundle and distribute the UIPSnap library binaries along with any of their applications.
To order a key now, use this link.
The license key will be sent by email to the address provided by PayPal. If you don't want that, please use the optional message box of PayPal to provide another address or wite a separate email before payment!
Learn how to use UIPSnap in 5 minutes
Download the library and copy the UIPSnap.dll into your VS Windows Forms project folder. Then add a project reference to the dll.
Note that only properties of tagged controls are handled! You need to select each control in the form's edtior and enter a number into the Tag field. The number defines the order in which controls are handled! If the order is not important, just set every tag field to 0 (see fig. below).
Another thing: Each control must have a unique Name in the UI tree!
Use the methods register and provide the key sent to you by mail after you ordered it.
You may define filters by the IncludeList and ExcludeList properties to control which properties are
Example (exclude width properties):
You may access the type list by the TypeList property to control which property types are handled.
Example (remove string type):
jReflectServer (formerly jReflect) has been updated. Version 2.0 allows distributed code ... Read MoreDocuCast
DocuCast is a real time file revision and backup system for use in professional and private ... Read MorejReflect: lightweight java web-server & -framework
jReflect is a very small & lightweight java web-server and -framework for ... Read MoreMore Blog Entries