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.
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 SettingsKey is the key feature that allows you to have multiple sets of settings.
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:
- Determine which application and user settings you wish to retain. For example, you may want to save the form size and location.
- Open the Project Designer for the Windows Application project.
- Select the Settings tab.
- 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.
- 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.
- 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.
- Define any default value in the Value column.
The result is shown in Figure 1.
Figure 1: The Settings tab of the Project Designer allows you to define user preference and application settings.
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.
To bind a setting to a property of a form or control at design time:
- Display the Properties window for the form or control.
- Open the (Application Settings) property in the Properties window.
- Map the desired property to the defined setting as shown in Figure 2.
Figure 2: The ApplicationSettings properties allow you to bind a setting to a specific form or control property.
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.
By: Deborah Kurata
Deborah Kurata is cofounder of InStep Technologies Inc., a professional consulting firm that focuses on turning your business vision into reality using Microsoft .NET technologies. She has over 15 years of experience in architecting, designing and developing successful applications.
Deborah is the author of several books, including Best Kept Secrets in .NET (Apress), Doing Objects in Visual Basic 6.0 (SAMS) and Doing Web Development: Client-Side Techniques (Apress). She is on the INETA Speaker’s Bureau, is a well-known speaker at technical conferences, and is a Microsoft Most Valuable Professional (MVP). After a hard day of coding and taking care of her family, Deborah enjoys blowing stuff up (on her XBox of course).
Some of the information in this article was obtained from her upcoming book, Doing Objects in VB 2005.
Users 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.