Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# - Settings ( Savable Variables )


  • Please log in to reply
5 replies to this topic

#1 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 11 August 2011 - 08:37 AM

Hello all, today we'll be going over Settings. I hope this tutorial covers all you'll need to know, and you understand how to use Settings to your advantage.


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


Here 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


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

  • 1
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#2 Epatron

Epatron

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 54 posts

Posted 11 August 2011 - 08:43 AM

I have been always dreamed of learning the C#!
Maybe someday I'll start learning it and do this!
Anyways looks good tutorial! *Thumbs up*
  • 0

#3 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 16 August 2011 - 10:11 AM

Nice tutorial - although I think you might have the definitions of user and application settings backwards?

The crucial distinction between application-scope and user-scope settings is that user-scope settings are read/write at run time, and their values can be changed and saved in code. Application-scope settings are read only at run time. While they can be read, they cannot be written to.

Using Settings in C#
  • 0

#4 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 16 August 2011 - 10:37 AM

I don't think there backwards? Let me say it a little clearer.

Me: User is a variable that can be assigned at design time(where we are now, code view) and be changed by the user at run time(while our program is running).
Msdn: User-scope settings are read/write at run time, and their values can be changed and saved in code.

Me: Application can only be assigned during design time, not runtime.
Msdn: Application-scope settings are read only at run time. While they can be read, they cannot be written to. Settings with application scope can only be changed at design time...

Does that make more sense? :)

~ Committed.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#5 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 16 August 2011 - 11:16 AM

Oh okay. I reread it and it makes sense. Don't know what I was thinking :P
  • 0

#6 Kriss

Kriss

    CC Regular

  • Member
  • PipPipPip
  • 29 posts
  • Location:Norway <3
  • Programming Language:C#, PHP, Visual Basic .NET
  • Learning:Python, Assembly

Posted 20 November 2015 - 04:42 AM

Wow, amazing! I've been struggling with this ever since I started with C#, finally. (I started 4 years ago) haha. Thanks man! +rep


  • 0