Question

Hi How can I save 2 last winform state? I use it when user clicks on "cancel" button. I wanna to set the form state into its proper one.

Thank you

Was it helpful?

Solution

You can save values of all relevant controls to the application configuration file, for example in FormClosing event (you'd need to create the properties yourself)

Properties.Settings.Default.textA = textBoxA.Text;

and restore them on form onLoad event

textBoxA.Text = Properties.Settings.Default.textA;

OTHER TIPS

Create a new instance of the form each time you need it.

  • On Form1_Load you set the data.
  • On btnSave_Click you save the data and close the form.
  • On btnCancel_Click you close the form.

If you can't create an instance of the form every time you could consider using a user control that you instanciate instead.

Part from that if you are using WPF you could bind GUI directly to datastructure.
In WinForms you can bind directly to some controls such as Grid/PropertyList/etc.
You can use Reflection to automaticaly iterate through the forms controls, but that is a bit complex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top