RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Retaining Multiple Sets of User Settings

Retaining a single set of user settings for your application is easy. Retaining multiple sets of user settings is much more challenging.

sers want their forms to be in the same position the next time they open their application. While Visual Studio 2005 out of the box provides a way to store one set of named settings, there is a way to store more.

Many Windows applications allow the user to define preferences that customize the application's appearance and behavior. One of the most common examples of user customizations is the ability to move and resize a form. The user can move or resize a form and then exit the application. When the user opens the application again, the form is restored to the last user-defined position and size.

The new settings feature in Visual Studio 2005 provides a very easy way to store these user preferences upon exit of the application and restore them when the user opens the application again. But these simple techniques only support one set of named settings. So if you have 20 forms in your application, do you need 20 named settings for the form location and 20 named settings for the form size? Luckily, the answer is no. With a little more work you can retain multiple sets of user settings.

This article first reviews the fundamentals of user settings. It then details how to work with multiple sets of user settings.

User Settings Fundamentals
A common user request is for your application to remember its state so that the application can be returned to that state when the user runs the application again. For example, if the user laid out the windows of the application in a specific manner and then closed the windows, the user expects that the windows will be returned to their last-defined locations and sizes when they are reopened.

Visual Studio 2005 added a Settings tab on the Project Designer window to support the management of application settings. Application settings allow you to store and retrieve property settings and other information for your application. You can use application settings to maintain custom application settings and end-user preferences.

You can use this feature for application settings that you don't want hard-coded into the application, such as your connection string. You can also use this feature to store end-user preferences such as form size and location and color choices. In either case, application settings are available within the code of your application using the My.Settings object.

To define an application setting:

  1. Determine which application and user settings you wish to retain. For example, you may want to save the form size and location.
    Figure 1. Project Designer Settings: The Settings tab of the Project Designer allows you to define user preference and application settings.
  3. Open the Project Designer for the Windows Application project.
  4. Select the Settings tab.
  5. In the Name column, enter the name of the setting. Each setting must have a unique name. For example, use FormLocation to define a setting for the location of the form and FormSize for the size.
  6. In the Type column, select the appropriate data type for the setting. You can select any data type that can be converted to a string.
  7. In the Scope column, define the scope: User or Application. Select User scope to define a setting that is saved for each user at runtime. Select Application scope to define a setting that is saved for the application.
  8. Define any default value in the Value column.
The result is shown in Figure 1.

There are some application settings, such as form location, that can be bound directly to a property of a form or control at design time. For Visual Basic applications, the runtime automatically stores the setting before the form is closed and automatically restores the setting when the form is opened again.

Figure 2. Using ApplicationSettings: The ApplicationSettings properties allow you to bind a setting to a specific form or control property.
To bind a setting to a property of a form or control at design time:

  1. Display the Properties window for the form or control.
  2. Open the Application Settings property in the Properties window.
  3. Map the desired property to the defined setting as shown in Figure 2.
Notice in Figure 2 that there are only two form properties that can be bound to user settings at design time. All other user settings must be set and restored in the code. The form size cannot be bound directly to a property because it requires additional code to handle the maximized and minimized case.

For form-specific user preferences, you can set the value of the setting before the form is closed. For the form size example, set the FormSize setting before closing the form as follows:

   Private Sub CustomerWin_FormClosing(ByVal sender _
      As Object, ByVal e _
      As System.Windows.Forms.FormClosingEventArgs) _
      Handles Me.FormClosing
      If Me.WindowState = FormWindowState.Normal Then
         My.Settings.FormSize = Me.Size
        ' If the form was maximized or minimized,
        ' return to the restore state
        My.Settings.FormSize = Me.RestoreBounds.Size
      End If
   End Sub
This code first checks the form state. If the WindowState is normal, you can simply store the form size. But if the WindowState is any other state, such as minimized or maximized, you must use the RestoreBounds size. Additionally, you could define a setting for WindowState so you can return the form to its original state.

You restore the user setting when the form is opened:

   Private Sub CustomerWin_Load(ByVal sender As _
      Object, ByVal e As System.EventArgs) _
      Handles Me.Load
      If My.Settings.FormSize <> _
         System.Drawing.Size.Empty Then
         Me.Size = My.Settings.FormSize
      End If
   End Sub
The settings are stored in two XML files: an app.config file, which is created at design time when you create the first application setting; and a user.config file, which is created at runtime when the user changes the value of any user setting. The user.config file is stored in:

   C:\Documents and Settings\\
      Local Settings\Application Data
This is called the local user profile. Alternatively, it will be stored in:

   C:\Documents and Settings\<your username>\Application Data
This is called the roaming user profile.

Author's Note: If you use ClickOnce deployment, the settings will be saved in the ClickOnce data directory and not in the local user configuration file.

The Visual Basic runtime automatically saves the user.config settings when the application is closed and retrieves the settings when the application is opened again. The userSettings section of the user.config XML file for the two settings defined here looks like this:

       <setting name="FormLocation"
         <value>57, 11</value>
       <setting name="FormSize" 
         <value>566, 362</value>
Use these new settings features whenever you have application properties that you don't want to hard-code into the application and when you want to store and retrieve a single set of user preferences.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date