Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a form that contains several text boxes, radio buttons, checkboxes etc. Right now I am saving their values respectively by declaring each one and saving to the programs settings:

Properties.Settings.Default.EmailFrom = txtbxEmailFrom.Text;

I would like to find a way to loop through all the objects and save their settings if possible so I don't have to declare each one individually.

Is there a way to do this? Or a better way to save the text of my textboxes, the checkstate of checkboxes & radiobuttons etc?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
128 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use property binding to application settings.

This way you can simply save settings by calling Properties.Settings.Default.Save(); and you don't need to loop over controls, because the properties are bound to settings and their values automatically push into settings on change.

You can bind properties to settings using designer or using code.

Using Designer

select your control at design time, then in property grid, under (ApplicationSettings) click ... for (PropertyBinding) and from the dialog, bind the properties you need to the settings.

Using Code

You can bind properties to settings, using code the same way you do it when data-binding using code:

this.textBox1.DataBindings.Add(
    new System.Windows.Forms.Binding("Text", Properties.Settings.Default, "Test", true,
        DataSourceUpdateMode.OnPropertyChanged));
        

Save Settings

To save settings, it's enough to call Save() on settings object some where like Closing event of form:

Properties.Settings.Default.Save();

Note

As an alternative to different controls for settings, you can also use a PropertyGrid to show all the settings and edit them.

More information:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...