Jump to content

Using Settings in VB.NET

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
Settings is used to store values from time to time the application runs. So they are like variables with a lifetime which doesn't end when the application closes.

To go to the setting window, right click on "My Project" in the solution window and then press Open:


[ATTACH]2311[/ATTACH]


Then make sure the checkbox with the text "Save My.Settings at Shutdown" is checked (number 1 at the image below) and then click at "Settings" in the list to the left(number 2 at the image below).


[ATTACH]2312[/ATTACH]


If you did everything right you should now see something like this:


[ATTACH]2313[/ATTACH]






Creating Settings


So here you have a list of all Settings you've created, for each setting you create you must set 4 things(marked with 1 to 4) Name, Type, Scope and Value.

  • Name - This is the name of your Setting, you'll use this to access your setting by your code. The name must be unique.
  • Type - This is the Data Type of your setting, some is already in the list but if you want others you have to browse for them(at the end of the list).
  • Scope - To difference to variables, settings have only two different "scopes". Either Application (which is readonly) or User(which is both readable and writable)
  • Value - This is the value your setting will start with the first time, laster on it could be change so the value of the setting doesn't have to be this every time it starts (which is also the reason we're using Settings). Some values is hard to write (Colors, Fonts for example) so therefor it often comes a button to allow you to browse your value(for colors a color viewer and for the font a font selector as an example). You don't have to set the value, if you leave it empty the setting will just start empty.


So to create a setting you simple set these 4 values, if you do a new empty line will appear, this "empty" line is not a setting even though it's filled with some default values.



Using Settings

To use the settings you've created you write "My.Settings.[the name of the setting]". Something like this(here's the setting is called Background):

[highlight=VB.NET]My.Settings.Background[/highlight]



To show an example on how we can use it I've created a Setting called Background of the type System.Drawing.Color with the Scope User and the value Blue. So now we have a color which we can edit (since its scope is user). I then add a button called cmdColor to the form and then I add this code:


[highlight=VB.NET] Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BackColor = My.Settings.Background
End Sub

Private Sub cmdColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdColor.Click
Dim color As New ColorDialog()
color.Color = My.Settings.Background
color.ShowDialog()
Me.BackColor = color.Color
My.Settings.Background = color.Color
End Sub[/highlight]

When our forms load, we set the background to the same color as our Background setting. And if the user clicks on the button a color dialog will be opened. When the user selects a color that color will be added as the background color but also saved in the Setting called background. So next time we start the form it will have the same color as when we closed it last time. Give it a try, start the form and select another color, then close the application and start it again, now it will still have the color you chose.



That was everything I had about Settings, Hope you learned anything useful from this tutorial :)

Attached Files



#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Cool! Does it save these settings in the Registry?

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
Yes :)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Nice +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
elixir_pr

elixir_pr

    Newbie

  • Members
  • Pip
  • 3 posts
Great post. Learned a very important thing from this little tutorial. I have always wanted to save a few user made settings when the application is closed. Thanks.

#6
Wanna-be-Programmer

Wanna-be-Programmer

    Newbie

  • Members
  • Pip
  • 9 posts
thanks that going to help me make my auto login!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users