开发者

.net: Best practice for saving 2 last Winform state?

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 on开发者_运维问答e.

Thank you


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;


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜