DejmiJohn
DejmiJohn

Reputation: 81

Reset part of application settings

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

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?

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 ?

Upvotes: 1

Views: 1737

Answers (4)

Nissim
Nissim

Reputation: 6553

Use the following solution to get the original value of a single setting:

(The example assumes you want to get the ORIGINAL value of a setting named 'Username')

var defaultUsername = Properties.Settings.Default.GetType()
                                                 .GetProperty(nameof(Properties.Settings.Default.Username))
                                                 .GetCustomAttribute<System.Configuration.DefaultSettingValueAttribute>()
                                                 .Value;

Important - this solution will always return a string value. make sure to parse it properly, or use this extension method I wrote:

public static T GetDefaultValue<T>(this ApplicationSettingsBase settings, string settingKey)
{
    return (T)Convert.ChangeType(settings.GetType()
                     .GetProperty(settingsKey)
                     .GetCustomAttribute<DefaultSettingValueAttribute>()
                     .Value, typeof(T));
}

Usage:

var defaultNumber = Properties.Settings.Default.GetDefaultValue<int>(nameof(Properties.Settings.Default.Number));

Upvotes: 2

Reza Aghaei
Reza Aghaei

Reputation: 125187

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. Create a method which resets all bound controls of a TabPage

  2. Using Multiple Settings Files with Designer Support

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

Here is the method:

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

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

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

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

Note for design-time support

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:

enter image description here

Upvotes: 1

Subash B
Subash B

Reputation: 164

Try this:

 private void button2_Click(object sender, EventArgs e)
    {
        TabPage page = tabControl1.SelectedTab;
        var controls = page.Controls;
        foreach (var control in controls)
        {               
            if(control is TextBox)
            {
              //do stuff
            }    
            if(control is ComboBox )
            {
                ComboBox comboBox = (ComboBox)control;
                if (comboBox.Items.Count > 0)
                    comboBox.SelectedIndex = 0;
                comboBox.Text = string.Empty;
            }                
        }
    }

Upvotes: 0

Mohamed Saeed
Mohamed Saeed

Reputation: 74

 TabPage page = aTabControl.SelectedTab;

        var controls = page.Controls;

        foreach (var control in controls)
        {
            //do stuff
        }

Upvotes: 0

Related Questions