Winforms/C#: Store/Retrieve (Persist) Content of TextBoxes between 2 Runs

六月ゝ 毕业季﹏ 提交于 2019-12-12 13:28:33

问题


Is there an easiest way to simply tell Winforms (mainly TextBoxes) to persist all its content e. g. to a file without me having to loop through all the controls?

I saw this in a WPF application, but am clueless (also, Google did not turn up anything) whether there is an out-of-the-box, easiest approach.

Again, looping through each control would be possible but seems.. too much work?

Would serialization maybe work?


回答1:


You can set your ApplicationSettings PropertyBinding in the Form Designer, assign it a Field Name then when you close your application you can do this which will save all of you changes to all of the bound textbox's in the applications app.config file.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Properties.Settings.Default.Save(); 
}



回答2:


You can use IsolatedStorageFile to save and load data. If you can bind your textboxes to a class with properties for each textbox, you can serialize the class and store it IsolatedStorageFile.

You can then deserialize from IsolatedFileStorage and the controls should be bound to the values present in the properties of the class.

For details about data-binding to a class refer to answeers to this question: Data Binding to an object in C#

Refer to this for further details about saving and loading to/from IsolatedStorageFile



来源:https://stackoverflow.com/questions/12927718/winforms-c-store-retrieve-persist-content-of-textboxes-between-2-runs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!