Retaining Multiple Sets of User Settings (Cont.) 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 Else ' 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\<your username>\ 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. 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: <userSettings> <PTWin.My.MySettings> <setting name="FormLocation" serializeAs="String"> <value>57, 11</value> </setting> <setting name="FormSize" serializeAs="String"> <value>566, 362</value> </setting> </PTWin.My.MySettings> </userSettings>
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. Retaining Multiple Sets of User Settings As currently defined, the FormLocation and FormSize example settings will only work for one form. For a consistent user interface, you would want the settings retained for each of the forms in your application. One way to define settings for each of your forms is to define a unique set of settings for each form. You could then use the techniques defined above. But that would be a significant amount of work, especially if your application has a large number of forms. A better approach is to create your own MySettings object and use the SettingsKey property to define a key for each form. To minimize the amount of repeated code required for this, add the code to a base form class as defined in my article, “Give Your Forms a Base” (CoDe Magazine March/April 2004). To define multiple sets of settings, begin by creating your own MySettings object. Insert a property into your base form class that will create and retain your specialized MySettings object: Private _settings As My.MySettings Private ReadOnly Property Settings() As _ System.Configuration.ApplicationSettingsBase Get If _settings Is Nothing Then _settings = New My.MySettings End If Return _settings End Get End Property
This code declares a private member variable to retain your custom MySettings object. It defines a private read-only property that will initially create an instance of your custom setting. It then returns the custom setting for use within the base form class. |