Arindam Sinha Online

August 15, 2009

User Preference using Application Settings


It’s very common that we need to provide custom functionalities in our applications to the users in many respect. So that the user can do some operations at runtime. Sometimes user can resize the form, user can change the color, user can change the text etc. Obviously if our application provides all such customizations then only user can do. Now the challenge comes – when user customizes the forms of our application and wants to hold that state when he restarts the application. Per industry’s terminology this is attributed as User Preferences. In this post I would cover a very basic and easy of doing that.

Original Look and FeelUser Preference - After Saving

Suppose, the original look and feel of the form of an application is shown in the above left image. This is the form developer has created with a bluish background and some text. Now the application provides an option to customize the Form’s as well as button’s properties to be modified by the user at runtime. Now user has preferred to save the reddish form and wants to load this next time onwards. I think this problem statement can site a User preference case study. In this post I would take this one as example and would solve this with source code as well.   

Approach for implementation of User Preferences

The better option would have been to store the Form’s state in a serialized format. As Forms are not serializable it would require custom serialization through out the application with some specific properties based on the requirement.

To have some generic approach with a small piece of custom coding we can achieve the same with Application Settings.

What are Application Settings?

Application settings allow a Windows Forms or ASP.NET application to store and retrieve application-scoped and user-scoped settings. A "setting", in this context, is any piece of information that may be specific to the application or specific to the current user – anything from a database connection string to the user’s preferred default window size. Application settings works by persisting data as XML to configuration (.config) files. In most cases, the application-scoped settings are read-only; because they are program information. By contrast, user-scoped settings can be read and written safely at run time.

If you use Visual Studio, you can define settings within the Windows Forms Designer using the (ApplicationSettings) property in the Properties window. Once the applications settings are created the wrapper class with all those setting are created automatically by Visual Studio.We will discuss later in detail how applications settings are added/deleted/accessed. Now if you want to create your own custom wrapper class to have more control over the settings then this can be achived by writing class inheriting from ApplicationSettingsBase class. Custom controls can also save their own settings by implementing the IPersistComponentSettings interface, which exposes the SaveSettings method.

Implement User Preferences

So far we have come to know what’s application scope and user scope. Now we will concentrate on user scope settings with the example shown in above screenshots.

In this example I have created one form on which a button is placed to save the User Preferences. In the form I kept one Tab control with two tab pages each has PropertyGrid to change the properties of Form and Button. First tab page is to change the properties of form whereas second one is for the button.

Create Application Settings

Now we will create some application settings with user-scope.

To create new Application Settings using Visual Studio –
  • Select the form or control whose properties you want to bind to the new application setting.
  • In the Property Editor, expand the (Application Settings) item, and then click the ellipsis next to the (PropertyBindings) item underneath it.

  • In the Application Settings dialog box, click the drop-down menu for the property you want to bind and select the New command.

  • In the New Application Setting dialog box, configure the setting by giving it a name (e.g. FormForeColor) and a default value (e.g. ControlText) and setting its scope (e.g. User).

For the form I have created some application settings as shown in the red marked section in the Property Grid. Also, for the button I have created few application settings like ButtonBackColor, SaveButtonText.

To Access Application Settings

Once this is done, a wrapper class Settings is created under the Properties folder. Any of these settings can be accessed by Settings class. Now for this example the the FormForeColor can be accessed as shown below.

UserPreferenceSample.Settings defaultSettings= new Settings();
defaultSettings.FormForeColor = this.ForeColor;
Save User Preferences

Till now we have the knowledge of the creation of application settings and accessing those. So it’s only the way you want to have your User Preferences to be implemented. In this example user can change the properties of Form and Button using the PropertyGrid. Also, I provided textboxes where user can provide his/her preferred texts for Form and the Button. Once user has modified all these and clicked the save button, Application Settings have been updated with the user’s selected ones.

//UserPreferenceSample.Settings defaultSettings= new Settings(); 
//- already instantiated
//Save button's settings

 defaultSettings.ButtonBackColor = btnSave.BackColor;

 defaultSettings.SaveButtonText = btnSave.Text; 
//Save the Form's Settings
 defaultSettings.FirstFormHeader = this.Text;

 defaultSettings.FormBackColor = this.BackColor;

 defaultSettings.FormForeColor = this.ForeColor;

 defaultSettings.FormOpacity = this.Opacity;

 defaultSettings.FormWindowState = this.WindowState; 


Now onwards, whenever user starts the application he/she can see preferred view.

Sample config File

Find the sample config file entries as shown below.

    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="UserPreferencesExample.Properties.Settings"   type="System.Configuration.ClientSettingsSection, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"   allowExeDefinition="MachineToLocalUser"   requirePermission="false" />
      <setting name="FirstFormHeader" serializeAs="String">
        <value>My First Form</value>
      <setting name="SaveButtonText" serializeAs="String">
      <setting name="FormWindowState" serializeAs="String">
      <setting name="FormForeColor" serializeAs="String">
      <setting name="FormOpacity" serializeAs="String">
      <setting name="FormBackColor" serializeAs="String">
      <setting name="ButtonBackColor" serializeAs="String">

Limitation of Application Settings

You cannot use application settings in an unmanaged application that hosts the .NET Framework. Settings will not work in such environments as Visual Studio add-ins, C++ for Microsoft Office, control hosting in Internet Explorer, or Microsoft Outlook add-ins and projects.

You currently cannot bind to some properties in Windows Forms. The most notable example is the ClientSize property.

Application settings has no built-in facility for encrypting information automatically. You should never store security-related information, such as database passwords, in clear text.


You can get the source code of the example here.

I always suggest and prefer to read MSDN. You can refer the followings articles which helped me a lot to understand this.


June 28, 2009

Databinding – Bindinglist,BindingSource and BusinessObjects – Part2

I have posted another article in CodeProject. This is mainly focussing on the Searching and Sorting of the BindingList.In general, these do not come in BindingList. This article would help you to understand how you can have Sorting and Searching in BindingList. I created a generic BindingList class and both the features are implemented. As we are use Business Entities and their collections in our applications, this generic BindingList would definitely reduce more coding stuff.

Please visit the following article –

Next Page »

Create a free website or blog at