Client Settings - Part 2

In a post a few months ago, I wrote about a new Whidbey feature called Client Settings. While we briefly described this feature at the PDC, it wasn't available in the build of Whidbey released then. The Community Preview build gives you a chance to try out everything I mentioned and more.

Quite some functionality has been added to this feature since the PDC. I list some of the new capabilities below. If you haven't read my previous post, please read that first for background.

[Caveat: Not all this may work very well in the recently released Community Preview build]

  • First of all, we now provide full fledged design time support in Visual Studio for you to easily create some settings and bind to them. As a simple example, lets say you want to provide the user of your app the ability to customize the BackColor of a Form, and you want to save their setting, so that when they run the app again, you can restore their preference. All you need to do is this: in the Form designer in Visual Studio, you will notice a new property in the property browser named “(Application Settings)”. This allows you to create a setting (in this case, user scoped) and set up a binding to a property in a manner very similar to setting up data bindings. In the background, the Settings Designer will create a class for you that derives from ApplicationSettingsBase that has a property corresponding to the setting you just created. And the Forms Designer will generate the code to data bind that setting to your BackColor property. All you need to do now is to call Save() on your settings object - say, in the Form's Closing event. This line, in VB, will look something like:

My.Settings.Save()

That's it! You now have everything setup to save each user's preference for BackColor of the Form.

  • The Settings Designer itself is another place you can start to create settings. In a VB project, the settings designer is offered as a tab in the Application Designer. In C#, you should be able get to it in a similar manner in a future build. The designer has simple UI to create settings and assign default values to them. It will generate the code for the settings class and its properties, with appropriate attributes specified.
  • ApplicationSettingsBase offers a bunch of new functionality. It has events that are fired when a setting's value changes, or when settings are saved (i.e., passed down to the provider). You can use these to do validation for example. It also provides methods to reset the settings to their default values and upgrade settings from a previous version of the settings class.
  • We also provide the ability for components to store their own settings, in a manner transparent to the host, through an interface called IPersistComponentSettings. The ToolStrip is an example of a control that implements this interface. If you drop a ToolStrip on your Form, you will notice a boolean property called 'SaveSettings'. Just set this to true, and the ToolStrip will automatically begin to store its own settings - like which rafting container it is placed in and what its location is within the container. ToolStrip items too can store out settings of their own. All this happens transparently - a ToolStrip user doesn't need to setup anything - just set SaveSettings to true! You can provide this same functionality for your component/control just by implementing IPersistComponentSettings.

So you may be wondering - where are these settings stored? That really depends on the settings provider you are using. The default settings provider that ships in Whidbey is called the LocalFileSettingsProvider. This stores all the settings in configuration files. One of the other things the Settings Designer described above does for you is put the required entries in the application config file, where our provider stores application scoped settings, and default values for user scoped settings.  In .Net Framework v1.1, there were two levels of configuration files - machine and application. In Whidbey, we have introduced the concept of user specific config files, that are stored under the user's profile path. These config files are created when a user saves their preferences, and contain the values that differ from the defaults.

Anyway, that's just an overview of some of the new functionality we have added since the PDC. If you are using the feature, do let us know what you think!