How to use Settings in VB.NET



we user Settings 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:


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)



when you click sittings you will this window:


So here you have a list of all Settings you've created, for every setting you create you must set 4 elements (marked with 1 to 4) Name, Type, Scope and Value
end here the definition for each one

1. Name - This is the name of your Setting, you'll use this to access your setting by your code. The name must be unique.

2. 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).

3. Scope - To difference to variables, settings have only two different "scopes". Either Application (which is read only) or User(which is both readable and writable)

4. 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 valu


how to use 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:
===========code==========
My.Settings.Background
=========================

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:

====================================code================================
Private Sub Mainform_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
=======================================================================


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.

this is it, if this helps you or not leave a comment , don't forget to like our Facebook page and follow us on twitter, thank you


Comments