如何保存不声明每个对象的所有形式的设置? [英] How to save all form settings without declaring each object?

查看:117
本文介绍了如何保存不声明每个对象的所有形式的设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含若干文本框,单选按钮,复选框等形式现在我通过声明每个并保存到程序设置分别保存它们的值:

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.

有没有办法做到这一点?或者更好的方式来拯救我的文本框的文本,复选框和放大器的checkstate;单选按钮等?

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

推荐答案

您可以使用的属性绑定以应用程序设置。

You can use property binding to application settings.

这样,你可以只需保存通过调用设置 Properties.Settings.Default.Save(); ,你不需要循环的控制,因为属性绑定到设置及其值自动推入上更改设置。

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.

使用设计师

选择您的在设计时的控制,然后在属性网格中,在(的applicationSettings)点击 ... (PropertyBinding),并从该对话框中,绑定你需要设置的属性。

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.

使用代码

您可以绑定属性,设置,使用代码你做同样的方式它在数据绑定使用代码:

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));

保存设置

要保存设置,这足以叫保存()上设置对象一些地方如结束形式的活动

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

Properties.Settings.Default.Save();

这篇关于如何保存不声明每个对象的所有形式的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆