Friday, December 17, 2010

Visual Basic Tips: Storing and Retrieving Application Settings in the Windows Registry

   
Each application / program has settings or configurations related to the application itself, such as password, the size of the form, and others. These settings must be stored into a storage to be taken back. Usually the application settings are stored in three places, namely on a file with .ini extension which is located in the same folder with program files, the program's database, or it can also be stored in the registry of Windows. This time, we will discuss how to store and retrieve settings into/from the Windows registry.

Visual Basic provides two functions for storing and retrieving data (configuration) into/from the registry, namely:

To retrieve data from the registry:

GetSetting(AppName As String, Section As String, Key As String, [Default]) As String

Explanation:
AppName = name of your application.
Section = can be called as a folder in the registry
Key = name of a setting
Default = returned value if the setting is not found. You may empty this.

To store data to the registry:

SaveSetting(AppName As String, Section As String, Key As String, Setting As String)

Explanation:
AppName = name of your application.
Section = can be called as a folder in the registry
Key = name of a setting
Setting = value of the setting

Position of the Data in the Registry

The position of the data stored in the registry if you use the above functions are stored at:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings

Usage Example

Here is a usage sample of the above functions. Here we will create a program that store the form position on the screen (into the registry) when the program is closed. Then when the program starts again, the form will show on the same position when the program closed.
Instructions: Create a program containing a form. The source code as follows:

Option Explicit

Private Sub Form_Load()
    Dim X As String
  
    'get Left value from Registry
    X = GetSetting("Test Registry VB", "form", "Left", "-")
    If X <> "-" Then Me.Left = CInt(X)
   
    'get Top value from
Registry
    X = GetSetting("Test Registry VB", "form", "Top", "-")
    If X <> "-" Then Me.Top = CInt(X)
   
End Sub

Private Sub Form_Unload(Cancel As Integer)
    'store Left and Top positions of the form into Registry
    SaveSetting "
Test Registry VB", "form", "Left", Me.Left
    SaveSetting "
Test Registry VB", "form", "Top", Me.Top
   
End Sub
 

Explanation:
From the above codes, we can see that we use two Form's event, the Load and Unload events. Load event occurs when the program starts executing (but Form doesn't show yet ). While the Unload event occurs when the program is closed.
To test the example above, please run the program first, then move the form to any position on the screen. Then close the program. After that, run the program again and see the form will show on the last position when the program was closed earlier.

After running the above example, you might want to see where the settings are stored in the registry. Please open the Registry Editor (Click the Start button, then Run. Then type regedit, and press ENTER). Open the following registry path:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings

You will see something like this:

registry windows visual basic
                   

Related Posts:

No comments: