Click here to Skip to main content
12,629,978 members (23,232 online)
Click here to Skip to main content
Add your own
alternative version

Stats

313.4K views
2.8K downloads
97 bookmarked
Posted

Application settings in VB.NET 2.0 and Visual Studio 2005

, 11 Jan 2006
Rate this:
Please Sign up or sign in to vote.
An article on using application settings in VB.NET 2.0 and Visual Studio 2005 to save the size and location of a form.

Introduction

A new feature of .NET 2.0 and Visual Studio 2005 is the ability to save a user's application settings in a user.config file that is saved in the user's desktop profile.

Background

Until .NET 2.0, it was difficult to save a user's settings for an application. User settings had to be saved in the registry, in an .ini file, or in a custom text file. Now, it is possible to save settings for a user's application that will even move with them in a roaming desktop profile.

To view the non-roaming settings, open the user.config file located at %USERPROFILE%\Local Settings\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<verison>\user.config.

To view the roaming user settings, open the user.config file located at %USERPROFILE%\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<verison>\user.config.

Using the code

To add application or user settings to a project, right-click on the project name in the Solution Explorer window, and click Properties. Then, click on Settings in the left tab list.

Sample Image - AppSettings2005.jpg

When a setting is added to the Visual Studio designer, a public property is created in the My.Settings namespace. Depending on the scope of the setting, the property will be ReadOnly or writable. This allows you to programmatically change the user setting values and save them with the My.Settings.Save() method.

A second way to save settings is to enable the 'Save My.Settings on Shutdown' setting in the application. To do this, right-click on the project name in the Solution Explorer window, and click Properties. Then, click on Application in the left tab list.

Sample Image - AppSettings2005_save.jpg

To restore the last saved dimensions of a form, we set the size and location of the form from the user settings in the form's Load event.

Private Sub Form1_Load _
    (ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Load
        'Set textboxes and form name from application and user settings

        'Notice how the application setting property is ReadOnly
        Me.Text = My.Settings.MainFormText

        'Notice how the user settings are writable
        Me.Size = My.Settings.MainFormSize
        Me.Location = My.Settings.MainFormLocation

        Me.txtFormText.Text = My.Settings.MainFormText

        'Show the form now
        Me.Visible = True
    End Sub

In the form's FormClosing event, we save the form's size and location values to the current values.

Main form closing

Private Sub Form1_FormClosing _
    (ByVal sender As Object, _
    ByVal e As System.Windows.Forms.FormClosingEventArgs) _
    Handles Me.FormClosing
        Try
            'Set my user settings MainFormSize to the
            'current(Form) 's size
            My.Settings.MainFormSize = Me.Size

            'Set my user setting MainFormLocation to
            'the current form's location
            My.Settings.MainFormLocation = Me.Location

            'Save the user settings so next time the
            'window will be the same size and location
            My.Settings.Save()

            MsgBox("Your settings were saved successfully.", _
            MsgBoxStyle.OkOnly, "Save...")
        Catch ex As Exception
            MsgBox("There was a problem saving your settings.", _
            MsgBoxStyle.Critical, "Save Error...")
        End Try
    End Sub

Conclusion

You can use application and user settings for many things in .NET 2.0. Just remember that if you want to change the settings then the scope has to be set as User.

History

  • Second revision - Moved around text and image to make the article more readable. Added a Conclusion section.
  • Third revision - Updated user.config file paths, and added profile and auto save on exit explanation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

itoleck
Help desk / Support www.swarmsoft.net
United States United States
Chad Schultz
13 years of Computer Information Systems experience
MCSE, MCAD, MCTS SharePoint

Blog: ChadSchultz.com

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionProblem with the Me.FormClosing Pin
tomk650528-Jun-12 16:38
membertomk650528-Jun-12 16:38 
QuestionHello how to make my.settings portable? Pin
leesong28-Feb-11 23:31
memberleesong28-Feb-11 23:31 
GeneralTweaking the code to handle window sizing better Pin
Martin Omander30-Sep-08 10:03
memberMartin Omander30-Sep-08 10:03 
QuestionHow do I change ConnectionString in app.config Pin
Leif Thomsen23-Aug-08 2:57
memberLeif Thomsen23-Aug-08 2:57 
QuestionIs it safe? Pin
Dan'M27-Oct-07 0:32
memberDan'M27-Oct-07 0:32 
AnswerRe: Is it safe? Pin
depmoddima2-Nov-07 1:00
memberdepmoddima2-Nov-07 1:00 
QuestionExpress Edition Pin
FREEYO17-Jul-07 16:19
memberFREEYO17-Jul-07 16:19 
Generalapplication settings in ToolStripMenuItem Pin
jhon edison20-Apr-07 19:25
memberjhon edison20-Apr-07 19:25 
GeneralRe: application settings in ToolStripMenuItem Pin
ctwalker16-Aug-07 5:30
memberctwalker16-Aug-07 5:30 
QuestionMy.Settings.Save for complex types? Pin
jerome51369-Apr-07 18:23
memberjerome51369-Apr-07 18:23 
GeneralRe: My.Settings.Save for complex types? Pin
emoreau18-May-07 8:30
memberemoreau18-May-07 8:30 
AnswerRe: My.Settings.Save for complex types? Pin
jerome513618-May-07 8:36
memberjerome513618-May-07 8:36 
AnswerRe: My.Settings.Save for complex types? Pin
jerome513618-May-07 8:37
memberjerome513618-May-07 8:37 
GeneralRe: My.Settings.Save for complex types? Pin
emoreau18-May-07 8:41
memberemoreau18-May-07 8:41 
QuestionIt's possible to have my.Setting from different classes? [modified] Pin
papervegetal29-Mar-07 23:29
memberpapervegetal29-Mar-07 23:29 
Questionpersist settings between versions Pin
Thomas Fang18-Dec-06 10:19
memberThomas Fang18-Dec-06 10:19 
AnswerRe: persist settings between versions Pin
ctwalker16-Aug-07 5:36
memberctwalker16-Aug-07 5:36 
QuestionWindows service is not reading the settings [modified] Pin
SvenCPA11-Jul-06 1:35
memberSvenCPA11-Jul-06 1:35 
QuestionRe: Windows service is not reading the settings Pin
UfiPhil20-Aug-06 18:20
memberUfiPhil20-Aug-06 18:20 
AnswerRe: Windows service is not reading the settings Pin
itoleck14-Sep-06 4:13
memberitoleck14-Sep-06 4:13 
GeneralRe: Windows service is not reading the settings Pin
DaveBlack25-Jan-11 3:32
memberDaveBlack25-Jan-11 3:32 
GeneralApplication Bindings Pin
dotslashmartyn9-May-06 14:42
memberdotslashmartyn9-May-06 14:42 
QuestionHow to handle application version change Pin
dscemama1-May-06 4:36
memberdscemama1-May-06 4:36 
AnswerRe: How to handle application version change Pin
majze15-Sep-06 21:11
membermajze15-Sep-06 21:11 
Questionwhat about C#? Pin
Paul Horstink29-Apr-06 2:29
memberPaul Horstink29-Apr-06 2:29 
AnswerHoly Crap! Pin
John Saccoccio9-Nov-06 8:26
memberJohn Saccoccio9-Nov-06 8:26 
GeneralRe: Holy Crap! Pin
Robert Jeppesen28-Mar-07 2:31
memberRobert Jeppesen28-Mar-07 2:31 
GeneralVB Only Focus Pin
JohnnySacks2-Apr-07 10:26
memberJohnnySacks2-Apr-07 10:26 
QuestionReference to a non-shared member requires an object reference Pin
7thgen3-Mar-06 5:18
member7thgen3-Mar-06 5:18 
QuestionChanging the user.config location Pin
tripix18-Feb-06 3:12
membertripix18-Feb-06 3:12 
AnswerRe: Changing the user.config location Pin
Kel_23-Aug-06 5:22
memberKel_23-Aug-06 5:22 
AnswerRe: Changing the user.config location Pin
krisg_198421-Jul-07 16:03
memberkrisg_198421-Jul-07 16:03 
GeneralC++/CLR Pin
aaronexodus11-Feb-06 12:59
memberaaronexodus11-Feb-06 12:59 
GeneralRe: C++/CLR [modified] Pin
molesmoke29-Aug-06 4:00
membermolesmoke29-Aug-06 4:00 
AnswerRe: C++/CLR Pin
molesmoke31-Aug-06 8:12
membermolesmoke31-Aug-06 8:12 
AnswerRe: C++/CLR Pin
VTSpoon28-Oct-06 13:38
memberVTSpoon28-Oct-06 13:38 
GeneralRe: C++/CLR to serialize enum struct Pin
semmel7118-Oct-07 7:36
membersemmel7118-Oct-07 7:36 
GeneralAutoSaveSettings Pin
Alberto Venditti13-Dec-05 5:43
memberAlberto Venditti13-Dec-05 5:43 
QuestionOn another computer Pin
Ofer Gal23-Nov-05 3:31
memberOfer Gal23-Nov-05 3:31 
AnswerRe: On another computer Pin
itoleck28-Nov-05 8:10
memberitoleck28-Nov-05 8:10 
GeneralRe: On another computer Pin
afrazier11-Jan-06 3:14
memberafrazier11-Jan-06 3:14 
GeneralRe: On another computer Pin
itoleck11-Jan-06 8:00
memberitoleck11-Jan-06 8:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 11 Jan 2006
Article Copyright 2005 by itoleck
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid