开发者

Implementing SAVE AND QUIT in Winforms App

开发者 https://www.devze.com 2023-02-06 21:03 出处:网络
I\'m building a Winforms application in C#.net 3.5 using Visual studio. Its essentially a questionnaire. The thing is I would like users to be able to fill out e.g. the first 3 out of the 9 steps (of

I'm building a Winforms application in C#.net 3.5 using Visual studio. Its essentially a questionnaire. The thing is I would like users to be able to fill out e.g. the first 3 out of the 9 steps (of the questionnaire), and be able to save and quit the app.

SORT OF LIKE PERSISTING THE STATE OF THE WINFORMS CONTROLS (e.g. checked radiobuttons & checkboxes, texts in textboxes, selections in dropdownlists/combo boxes, etc) remain so even after app has closed/exited.

So users should also be able to start the app at a later time and continue from step 3 (or whatever step they were on, the last time they logged out/exited the app). I imagine this should be possible as it i开发者_开发知识库s, with webforms apps. Any ideas or answers would be greatly appreciated!! :)


In short:

  1. Create a data class for your answers.
  2. Bind member variable of the class instance to the controls.
  3. On app exit, serialize class instance you used to the XML file (or binary or whatever).
  4. On next loading, de-serialize the data and binding will do the rest.

It might seem like you have to learn lots of stuff here, but it will pay off.


As Daniel pointed out Databinding could be the way to go. Alternatively if it isn't too many fields you are still allowed to do manual assigning on load and on save. (nobody will shoot you for using simple code for a simple task)

Here is one article on how databinding can be solved: http://www.15seconds.com/issue/040614.htm
And here is a nice article which goes into detail on databinding: http://www.akadia.com/services/dotnet_databinding.html

Serialization is a way of making an object into a string/byte-strem/whatever. For example you could save an object as an xml-file and load it back later. It is very easy to use. You can read more about it here: http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file

Sample of serializing:

Stream stream = File.Open("filename.bin", FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, objectToSerialize);
stream.Close();

If you want to save it as XML you simply use XmlFormatter instead.

Hint: You can process the Form_Closing event to query user if he wants to save/quit. Set "e.Cancel = true;" to keep the form open.

0

精彩评论

暂无评论...
验证码 换一张
取 消