Suppose that we have a form with different controls. let say a textbox and a chkbox. If a user types something and change check status, I would like that by closing the form and opening it again, I'll be able to see the status of the form just before closing(not the designed status). I need this to implement on a form with around 50 different controls. Would you please give me a hint for that? by the way I am programing in C#.
2 Answers
Edit: New Answer:
Recursively find all child controls of the form that are TextBox and CheckBox and save them (in a dictionary[name,value] or to a file or database, your choice). Then, when it comes time to show the form again, reverse the process. Recurse through the children, and find the previous value of the control from your list. If your old file does not contain the value, use the default (don't set the Text/Checked properties).
Original Answer: Prevent the form from really closing, so in Form
protected override OnClosing (CancelEventArgs ea)
{
ea.Cancel = true;
this.Hide ();
}
Then, when you need to show the form again,
myForm.Show ();
This will preserve everything exactly as it was.
-
Thanks, but what I need is somehow save the form. Since I need to get the user one of the following options: 1. Create New 2. Edit Existing. The Form actually gets the adjustments for running a simulation, so its difficult to adjust the preferences every time. Also different adjustments should be loadable in future. – Amir Jul 12 '11 at 23:59
-
-
Yes That will help. however I have, user defined controls,and combo boxes that are populated by user, and there are different event interactions between different controls. I was thinking about serialization. is there any thing that simply I could write the form to a file with all its complications? – Amir Jul 13 '11 at 00:25
-
If you're talking about [Serializable], ISerializable, BinaryFormatter, etc. then, No, there is not a simple solution to this problem. That would be nice, but it would be way too complicated. – agent-j Jul 13 '11 at 00:35
-
Allright then, thanks, I just wanted to make sure, there is not any simple saving method. – Amir Jul 13 '11 at 00:44
I think the proper way to do it so to define a data model which holds all the information required by the form. Then you can persist that data model (database, xml, ...) and load it again. When you open the form and the current user has not completed the form then you can fill the form values based on the data model. This decouples you data (which I presume you are actually interested in for your business logic) from the view. You might want to have look at Model-View-Presenter design pattern.

- 18,612
- 4
- 58
- 83