What is the best way to implement a "program options" dialog with the "Reset to default" capability in C# (vs2005)?
Below is the way I am doing.
- Create a dialog form and add some controls (like checkBoxes) to it. Also add three buttons: Ok, Cancel, Default.
- Create Settings file and add some fields in the user scope.
- Bind the dialog controls with the corresponding settings fields through the "Application Settings" properties in the Visual Studio property dialog.
Add some code to the owner form:
if (myDialog.ShowDialog(this) == DialogResult.OK)
{
MySettings.Default.Save();
}
else
{
MySettings.Default.Reload();
}Add the following line in the DefaultButtonClick event in the Dialog Form:
MySettings.Default.Reset();
Note: Save(), Reload(), Reset() are common .Net functions of the ApplicationSettingsBase class. The detailed explanation on this can be seen at http://www.codeproject.com/KB/dotnet/user_settings.aspx (thanks BillW for the link).
This code works perfectly, save and restore the user setting without problem, but "reset to default" functionality differs from what I see in many popular software. In my implementation "Reset" cannot be canceled (because Settings.Default.Reset() cannot be reverted back), however if you see an option dialog of some popular program (for example, "Folder options" in the Windows Explorer), reset can be canceled by pressing Cancel button.
So, what is the best and simple way to implement the "traditional" way of the "Reset" functionality?
Update (and probably the best answer)
Currently I have resolved the problem the following way. Instead of
MySettings.Default.Reset();
that cannot be reverted back, I read of the default values directly like this:
MySettings.Default.MyBoolValue = bool.Parse((string)MySettings.Default.Properties["MyBoolValue "].DefaultValue);
Now all works just the way I wanted, but actually I feel this code to be a little bit dirty, because I need to do this for each variable individually, perform type converting, and so on. If somebody knows the better simple solution, please post here.