重置部分应用程序设置 [英] Reset part of application settings

查看:168
本文介绍了重置部分应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个名为"Preferences"的表格,其中带有TabControl.此TabControl包含几个TabPages(常规,高级,杂项等),带有很少的组合框,复选框和标签. TabPage内部的每个控件都被分配了应用程序设置属性绑定(又名显示应用程序设置属性绑定" ).

So, I have Form called Preferences with TabControl in it. This TabControl contains several TabPages(General, Advanced, Misc, ...) with few comboboxes, checkboxes and labels. Each of this control inside TabPage is assigned Application Settings Property Binding (aka they show saved user settings, user can change them etc...).

我知道有一种方法可以重置所有设置(Properties.Settings.Default.Reset();),但是有一种方法可以仅重置一个TabPage中的设置吗?

I know that there is a method to reset all settings (Properties.Settings.Default.Reset();), but is there a way how to reset only settings inside one TabPage?

我的解决方案是遍历TabPage中的控件,检查它是否是组合框,标签等,然后将其值重置为默认值,但是对此是否有单一"解决方案?

My solution is to iterate thru controls in TabPage, check if it is combobox, label etc and then reset it´s value to default, but is there a "oneliner" solution to this ?

推荐答案

ApplicationSettings不具有仅支持重置某些属性的内置支持.但是要解决该问题,可以使用以下两个选项之一:

The ApplicationSettings doesn't have built-in support to reset just some properties. But to solve the problem, you can use either of these options:

  1. 创建一个方法来重置TabPage

在Designer支持下使用多个设置文件

Using Multiple Settings Files with Designer Support

选项1-创建一种方法来重置TabPage的所有绑定控件

您可以创建一个方法来查看选项卡页的控件,并检查它是否绑定到应用程序设置,在设置中找到该属性并将其值重置为默认值.然后,您可以重置TebPage宽度的一行代码的设置:ResetSettings(tabPage1);.

Option 1 - Create a method which resets all bound controls of a TabPage

You can create a method which look at controls of the tab page and check if it's bound to application settings, find the property in settings and reset its value to the default value. Then you can reset settings of a TebPage width one line of code: ResetSettings(tabPage1);.

这是方法:

private void ResetSettings(TabPage tabPage)
{
    foreach (Control c in tabPage.Controls)
    {
        foreach (Binding b in c.DataBindings)
        {
            if (b.DataSource is ApplicationSettingsBase)
            {
                var settings = (ApplicationSettingsBase)b.DataSource;
                var key = b.BindingMemberInfo.BindingField;
                var property = settings.Properties[key];
                var defaultValue = property.DefaultValue;
                var type = property.PropertyType;
                var value = TypeDescriptor.GetConverter(type).ConvertFrom(defaultValue);
                settings[key] = value;
                //You can also save settings
                settings.Save();
            }
        }
    }
}

选项2-在设计者支持下使用多个设置文件

如果使用单个设置文件的原因是由于设计者的支持,那么您应该知道您也可以使用多个设置文件获得设计者的支持.然后,您可以使用不同的设置文件并分别重置每个设置组.您可以使用以下代码将它们简单地封装在一个类中:

Option 2 - Using Multiple Settings Files with Designer Support

If the reason of using a single settings file is because of designer support, you should know you can have designer support also with multiple settings files. Then you can use different settings files and reset each settings group separately. You can simply encapsulate them in a single class using such code:

public static class MySettings
{
    public static Sample.General General
    {
        get { return Sample.General.Default; }
    }
    public static Sample.Advanced Advanced 
    {
        get { return Sample.Advanced.Default; }
    }
    public static void ResetAll()
    {
        General.Reset();
        Advanced.Reset();
    }
    public static void SaveAll()
    {
        General.Save();
        Advanced.Save();
    }
}

要重设设置组,只需调用MySettings.General.Reset();

To reset a setting group it's enough to call MySettings.General.Reset();

要重置所有设置,您可以拨打MySettings.ResetAll();

To reset all settings, you can call MySettings.ResetAll();

设计时支持说明

要让设计人员支持将属性绑定到设置,请在项目的根目录中创建多个设置文件.不要将设置文件放在文件夹中.设置选择器仅显示Properties文件夹中的Settings.settings文件以及项目根目录中的那些文件.这样,您可以在树状视图中看到不同的设置文件和设置属性,如下所示:

To have designer support for binding properties to settings, create multiple settings files in root of your project. Don't put settings files in folders. The setting picker, only shows Settings.settings file which is in Properties folder and those files which are in root of project. This way you can see different settings files and settings properties in a tree-view like this:

这篇关于重置部分应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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