What is a Setting?
A Setting, in a nutshell, is a savable variable. You can set the value of the Setting and once you restart your application it doesn’t lose or change its value, pretty handy.
When should I use settings?
Settings should be used when you have a variable you need to keep track of for your user. Example uses are, color Scheme’s, pop up tips, auto updating. All of which need a variable that does not lose it value after a restart.
Also note that, Settings should not be used as a database to hold large amount of variables.
Making your first Setting.
So to create a Setting variable you'll need to create new project, naming it however you please. Once done go ahead and add 1 button and 1 ColorDialog(for this tutorial we'll be change the color Scheme, as mentioned).
With both controls added, please double click the button and add.
colorDialog1.ShowDialog();
this.BackColor = colorDialog1.Color;
Simple enough, change the form color to the chosen colorDialog color. Run your program and make sure all works. As you run your program you may notice that no matter what color you change the form to, it always resets to its default gray once it’s closed and restarted. To fix this we must create a Setting variable to hold our color. To do this, right click your project in the solution explorer and click properties.(You can aslo use Project>Properties. from the menu of VS) You will now see to the properties window, from which you should click the Settings tab.
C#-settings.png 57.85K
84 downloadsHere we are! This is where we will be creating our settings. On this window you will see the setting variables 4 properties. 3 of the 4 you should understand, the only one you may not is the scope.
The scope has two values, "User" or "Application". User is a variable that can be assigned at design time(where we are now, the code view) and be changed by the user at run time(while our program is running). Application can only be assigned during design time, not run time(read only at run time). So for this tutorial we want the user to be able to change the Settings variable, thus we leave it at "User". Here is what my settings now looks like.
C#-setting-value.png 9.02K
35 downloadsAs you can see I named it "formColor" and changed its type to "System.Drawing.Color" so it can hold color data.
Great. Now we have a setting, let’s assign some values.
Reading & Changing Setting values.
Open up your button code once again, and add.
Properties.Settings.Default.settingVariableName = colorDialog1.Color; Properties.Settings.Default.Save();
First we access the variable through the project properties into default settings where the variable is, once accessed we assign the Setting value to the color chosen from the dialog, then we save the value so we can use it even after the program is restarted. Now when you pick a color its saved in our Settings variable, but we still need to get the value on startup to change the form back to its saved color.
In your Form() function add this code.
this.BackColor = Properties.Settings.Default.settingVariableName;We simply assign the forms backColor to the value of the setting variable. Tada, your form now saves and restores the chosen color scheme from the Setting variable.
You can make the setting variable any type you want to save data for lots of situations, just don’t for get to use the Save() after any changes. I hope this tutorial was informative and easy to understand, any questions, comments, or rep welcome.
Thanks ~ Committed.
Edited by CommittedC0der, 16 August 2011 - 11:00 AM.


Sign In
Create Account


Back to top









