What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleUse strongly typed program settings to save and restore settings in Visual Basic 2005
DescriptionThis example shows how to use strongly typed program settings to save and restore settings in Visual Basic 2005.
Keywordsprogram settings, settings, save settings, Visual Basic, VB.NET
CategoriesSoftware Engineering
Visual Basic's SaveSetting and GetSetting methods let you easily save and restore settings in the Registry but that's not the only place you can store settings.

Program settings have the advantage that they are strongly typed and they should also work on even on platforms that do not have a Registry, such as a Unix system using Mono to run a Visual Basic .NET application. (Although I haven't tried this because I don't have Unix installed. If you try it, let me know how it works.)

To make the settings, open the project's Properties pages and select the Settings tab. Create Integer settings named Left, Top, Width, and Height. Give them some reasonable default values to use when the program first starts.

When the program starts, the following code loads the form's settings.

' Reload the previously saved settings.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Me.Left = My.Settings.Left
    Me.Top = My.Settings.Top
    Me.Width = My.Settings.Width
    Me.Height = My.Settings.Height
End Sub
When the program ends, it uses the following code to save its current position and size.
' Save the current settings.
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.FormClosedEventArgs) Handles _
    My.Settings.Left = Me.Left
    My.Settings.Top = Me.Top
    My.Settings.Width = Me.Width
    My.Settings.Height = Me.Height
End Sub
Remember that the settings are strongly typed so IntelliSense knows about them and can ensure that you set them to values of the proper data type.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.