+ Reply to Thread
Results 1 to 6 of 6

Thread: Using Settings in VB.NET

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Using Settings in VB.NET

    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:


    Using Settings in VB.NET-recources1.png


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


    Using Settings in VB.NET-settings2.png


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


    Using Settings in VB.NET-settings3.png






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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Using Settings in VB.NET

    Cool! Does it save these settings in the Registry?

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Using Settings in VB.NET

    Yes

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Using Settings in VB.NET

    Nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    elixir_pr is offline Newbie
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Re: Using Settings in VB.NET

    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.

  7. #6
    Join Date
    Jul 2011
    Location
    Ithaca, Michigan, United States
    Posts
    9
    Rep Power
    0

    Re: Using Settings in VB.NET

    thanks that going to help me make my auto login!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. problem using settings in vb.net
    By Paulo_Jorge in forum Visual Basic Programming
    Replies: 11
    Last Post: 04-27-2011, 09:40 AM
  2. DNS Settings for Internet and Intranet
    By newphpcoder in forum PHP Development
    Replies: 1
    Last Post: 12-02-2010, 08:15 PM
  3. Hidden Settings in Safari 4
    By Ricardo-san in forum Tutorials
    Replies: 3
    Last Post: 04-16-2009, 08:31 PM
  4. change router settings
    By nrodes in forum General Programming
    Replies: 3
    Last Post: 02-07-2009, 07:00 PM
  5. Help profile settings
    By ahmed in forum C# Programming
    Replies: 5
    Last Post: 12-03-2008, 01:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts