Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Program options" dialog with the "Reset" capability - best practice
    text
    copied!<p>What is the best way to implement a "program options" dialog with the "Reset to default" capability in C# (vs2005)?</p> <p>Below is the way I am doing.</p> <ol> <li>Create a dialog form and add some controls (like checkBoxes) to it. Also add three buttons: Ok, Cancel, Default.</li> <li>Create Settings file and add some fields in the user scope.</li> <li>Bind the dialog controls with the corresponding settings fields through the "Application Settings" properties in the Visual Studio property dialog.</li> <li><p>Add some code to the owner form:</p> <p>if (myDialog.ShowDialog(this) == DialogResult.OK)<br> {<br> MySettings.Default.Save();<br> }<br> else<br> {<br> MySettings.Default.Reload();<br> } </p></li> <li><p>Add the following line in the DefaultButtonClick event in the Dialog Form:</p> <p>MySettings.Default.Reset();</p></li> </ol> <blockquote> <p><strong>Note:</strong> Save(), Reload(), Reset() are common .Net functions of the ApplicationSettingsBase class. The detailed explanation on this can be seen at <a href="http://www.codeproject.com/KB/dotnet/user_settings.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/dotnet/user_settings.aspx</a> (thanks BillW for the link).</p> </blockquote> <p>This code works perfectly, save and restore the user setting without problem, <strong>but</strong> "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.</p> <p>So, what is the best and simple way to implement the "traditional" way of the "Reset" functionality?</p> <p><strong>Update (and probably the best answer)</strong></p> <p>Currently I have resolved the problem the following way. Instead of</p> <pre><code>MySettings.Default.Reset(); </code></pre> <p>that cannot be reverted back, I read of the default values directly like this:</p> <pre><code>MySettings.Default.MyBoolValue = bool.Parse((string)MySettings.Default.Properties["MyBoolValue "].DefaultValue); </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload