G_Waddell 131 Posting Whiz in Training

Hi,

Ever had an application that you want to store user specific settings on? e.g. login name or Form background colours, User specifc DSN connections etc. So the next time they run the app the information is there for them?

This can be achieved in VB.net with just a few easy steps.
Set up your user level application setting.
Write values to it
Read those values back

1. Setting Up User Scoped Application
In the Solution Explorer Window, select your project and right click. Select the Properties option in the sub menu.
In the Properties Screen there should be a Settings tab, open it.
You should now see a grid with the following columns:
Name The Name you wish to give the settting
Type The Type of settng value e.g. String, Integer etc.
Scope User or Application, Pick USER as this is user specific and can be modified programatically while the application is running.
Value A default value for the setting

Application Scope is a system wide fixed value that writes to app.config which can be changed programmatically but this change can not be accessed until the project is reloaded. User Scope writes to a file in C:\Documents and Settings and can be accessed at any time by the program for the specific user.

2. Write Values to your Setting
As can be seen in the Example Sub StoreSettings once you have carried out step one above, you can read and write to these settings through the Settings Class.

To write to the setting simply use
My.Settings.SettingName = New Value

Important in order to store / keep these changes after the application closes you must save the update with
My.Settings.Save

Also note these settings are saved to a file text and are therefore accessible locally I DO NOT recommend you store passwords in them...

  1. Reading Those Values back
    As you can see from my example sub GetSettings, all you do is have the setting on the Right of the = e.g.
    MyVar = My.Settings.MySettingName

NOTE if for what ever reason, you wish to reset a setting to the default, then the original settings can be restored from the App.config file by calling
My.Settings.Reload

I hope this helps someone out there....

Sub StoreSettings(byRef UserName as String, byRef bgColour as String)
    With My.Settings
         .UserName = UserName
         .FormColour = bgColour
         .Save()
    End With
End Sub

Sub GetSettings()
    dim UserName as String
    dim bgColour as String
    UserName = My.Settings.UserName
    bgColour = my.Settings.FormColour   




End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.