Question

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?

Was it helpful?

Solution

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.

enter image description here

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

OTHER TIPS

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

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