tags:

views:

6028

answers:

6

I have a number of application settings (in user scope) for my custom grid control. Most of them are color settings. I have a form where the user can customize these colors with a button for reverting to default color settings. By default value I mean what I set in Settings.settings at design time. Is there a way to read the default settings from Properties.Settings? I was hoping there would be something like:

Color defCellColor = Properties.Settings.Default.CellBackgroundColor.DefaultValue;

or even:

Color defCellColor = (Color)Properties.Settings.Default.GetDefaultValue("CellBackgroundColor");

For example:

  1. I have a user setting named "CellBackgroundColor" in Properties.Settings.
  2. At design time I set the value of CellBackgroundColor to Color.White using the IDE.
  3. User sets CellBackgroundColor to Color.Black in my program.
  4. I save the settings with Properties.Settings.Default.Save();
  5. User clicks on the "Restore Default Colors" button.

Now, Properties.Settings.Default.CellBackgroundColor returns Color.Black. How do I go back to Color.White?

A: 
Properties.Settings.Default.<property> // returns default value for <property>
aku
A: 

How do I go back to Color.White?

Two ways you can do:

  • Save a copy of the settings before the user changes it.
  • Cache the user modified settings and save it to Properties.Settings before the application closes.
John
+7  A: 

@ozgur,

Settings.Default.Properties["property"].DefaultValue // initial value from config file

Example:

string foo = Settings.Default.Foo; // Foo = "Foo" by default
Settings.Default.Foo = "Boo";
Settings.Default.Save();
string modifiedValue = Settings.Default.Foo; // modifiedValue = "Boo"
string originalValue = Settings.Default.Properties["Foo"].DefaultValue as string; // originalValue = "Foo"
aku
Typo - "Boo" should be "Bar" in second line of example :)
Stomp
+1  A: 

I've got round this problem by having 2 sets of settings. I use the one that Visual Studio adds by default for the current settings, i.e. Properties.Settings.Default. But I also add another settings file to my project "Project -> Add New Item -> General -> Settings File" and store the actual default values in there, i.e. Properties.DefaultSettings.Default.

I then make sure that I never write to the Properties.DefaultSettings.Default settings, just read from it. Changing everything back to the default values is then just a case of setting the current values back to the default values.

Matt Warren
I also prefer this approach, since calling {Settings.Default.Properties["Foo"] as string} can result in an exception if the settings name is changed. Also, the casting can fail. I think between duplication of data (user default and app default) and type safety, duplication is the lesser evil here
ohadsc
BTW, making sure you never write to the default settings is easy - make all settings there application scoped, rather than user scoped
ohadsc
+2  A: 

Reading "Windows 2.0 Forms Programming", I stumbled upon these 2 useful methods that may be of help in this context:

ApplicationSettingsBase.Reload

ApplicationSettingsBase.Reset

From MSDN:

Reload contrasts with Reset in that the former will load the last set of saved application settings values, whereas the latter will load the saved default values.

So the usage would be:

Properties.Settings.Default.Reset()
Properties.Settings.Default.Reload()
ohadsc
A: 

Properties.Settings.Default.Reset() will reset all settings to their original value.

Domagoj Peharda