Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Retaining Multiple Sets of User Settings : Page 2

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


advertisement
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).

The SettingsKey is the key feature that allows you to have multiple sets of settings.
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 and 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.

Next you'll create a method in your base form class to apply the settings from your custom MySettings object. This method will be called when the form is loaded to apply the settings to your form, basically resetting the form size and location to the saved settings.

''' <summary> ''' Applies the settings for every form ''' </summary> Private Sub ApplySettings() Settings.SettingsKey = Me.Name Dim theSettings As My.MySettings theSettings = DirectCast(Settings, _ My.MySettings) If theSettings.FormSize <> Size.Empty Then Me.Size = theSettings.FormSize End If If theSettings.FormLocation <> Point.Empty Then Me.Location = theSettings.FormLocation End If End Sub

The first line of this method sets the SettingsKey. The SettingsKey is the key feature that allows you to have multiple sets of settings. Each set of settings has a unique SettingsKey. In this case, it is assigned as the name of the form to associate the settings with the particular form.

The next two lines cast your Settings property to your MySettings object. This maps the appropriate settings to your Settings property so the SettingsKey must be set prior to performing this cast.

The casting is required because the property is of type System.Configuration.ApplicationSettingsBase. In order to access your specific properties, you need to cast it to your MySettings object.

Author's Note: The MySettings class is generated by Visual Studio and can be found in the My Project subdirectory for the project in the Settings.Designer.vb file.

The remainder of this method is similar to the code defined earlier for applying the settings. When defining your own settings, you cannot bind the settings with properties in the Properties window. You instead need to manually set the properties. You also need to create a method in your base form class to save the settings in your custom MySettings object. This method will be called when the form is closed to save the settings.



''' <summary> ''' Saves the settings for each form ''' </summary> Private Sub SaveSettings() Settings.SettingsKey = Me.Name Dim theSettings As My.MySettings theSettings = DirectCast(Settings, _ My.MySettings) If Me.WindowState = FormWindowState.Normal Then theSettings.FormSize = Me.Size Else ' If the form was maximized or minimized, ' return to the restore state theSettings.FormSize = Me.RestoreBounds.Size End If theSettings.FormLocation = Me.Location Settings.Save() End Sub

The first line of this method sets the SettingsKey to the name of the form. This ensures that the settings are associated with the particular form. The next two lines cast your Settings property to your MySettings object. This maps the appropriate settings to your Settings property so the SettingsKey must be set prior to performing this cast.

The casting is required because the property is of type System.Configuration.ApplicationSettingsBase. In order to access your specific properties, you need to cast it to your MySettings object.

The next set of code sets the FormSize setting to the size of the form. The code to manually set the FormLocation setting is also defined because it was no longer set automatically using the Properties window. The last line of code saves your settings. The automatic saving of settings described earlier does not save your custom settings.

The last step is to modify the form Load and FormClosing events in the base form class to call these two methods. The form Load event handler is as follows:

Private Sub PTBaseWin_Load(ByVal sender _ As Object, ByVal e As System.EventArgs) _ Handles Me.Load ' Ensure the form is restored appropriately as ' per the settings ApplySettings() End Sub

The form FormClosing event handler is:

Private Sub PTBaseWin_FormClosing(ByVal sender _ As Object, ByVal e As _ System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing ' Save the form's settings SaveSettings() End Sub

To try this out, add the Settings property and ApplySettings and SaveSettings method to a base form class (or directly into one of your forms). Then call the two methods in the form Load and FormClosing events. Run the application and display one of your forms (a form that inherits from the base form class, if you added the code to a base form class; otherwise the form containing the settings code). Size and move the form then close the application. If all is well, when you reopen the application the form should resize and reposition itself.

If you had a ProductWin form and CustomerWin form that used this settings code, the resulting user.config file will look similar to this:

<userSettings> <PTWin.My.MySettings.ProductWin> <setting name="FormLocation" serializeAs="String"> <value>189, 2</value> </setting> <setting name="FormSize" serializeAs="String"> <value>434, 359</value> </setting> </PTWin.My.MySettings.ProductWin> <PTWin.My.MySettings.CustomerWin> <setting name="FormLocation" serializeAs="String"> <value>50, 140</value> </setting> <setting name="FormSize" serializeAs="String"> <value>184, 190</value> </setting> </PTWin.My.MySettings.CustomerWin> </userSettings>

Notice that there are now specialized PTWin.My.MySettings tags for both ProductWin and CustomerWin. Any form that inherits from the base form class will get a section in this user.config file to define its settings.

Author's Note: You can clear your user.config files at any time by clicking the Synchronize button on the Settings tab of the Project designer as shown in Figure 1. This deletes the user.config files. They will then be recreated the next time you run the application.


By creating your own settings object and using the SettingsKey, you can define multiple sets of settings. The code in this article used this feature to implement a set of settings for each form in the application, but you can use this same technique any time you want to assign multiple sets of settings.

As another example, a requirement of your application may be to allow the user to modify the parameters for any grid on any form. The user can define filter criteria, sort any column, and resize columns. You could use the SettingsKey to define a unique key for each grid. Then you can store and reapply settings for each grid.



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 .NET applications. Deborah is the author of several books, including "Doing Objects in Visual Basic 6.0" (SAMS) and "Doing Web Development: Client-Side Techniques" (APress). She is on the INETA Speaker's Bureau and is a well-known speaker at technical conferences.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

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