RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Use Windows Forms Application Settings to Personalize Your Applications

Application Settings is a new feature in Windows Forms 2.0 that allows you to maintain both user- and application-specific data (known as settings). Find out how to use it to improve your application's usability with personalization.

ne way to improve the usability of your application is to allow personalization—where different users of the same application have their own set of personalized settings. To do this, you need a way to store user-specific data, such as the size of the Window, the background color, etc. And while some data should be user-specific, some data applies across the board—things like database connection strings, Web services URLs, etc.

In Windows Forms 2.0, a new feature known as Application Settings provides a good solution to the personalization problem, by allowing you to maintain both user-specific and application-specific data (known as settings). This article takes you on a tour of this new feature and how you can implemented it in C#.

Creating the Sample Project
Using Visual Studio 2005, create a new C# Windows application and name it AppSettingsInCS. Right-click on the project name in Solution Explorer and select Properties. In the project designer page, click on the Settings tab.

Figure 1. Settings: Defining the three settings of different types.

User Scope
The first few settings you will create are of user scope. You will see what this means shortly. In the grid on the Settings page, first add the FormBackgroundColor setting and set its Type to System.Drawing.Color. For the Scope property, set it to User. You will notice that the default color is actually white (represented by the square box); you can change its value by selecting the drop down list. Figure 1 shows the other two settings (and their corresponding types, scope, and values) that you will create—FormSize and FormPosition. Remember that it's important to initialize the settings with a default value. If not, accessing them during runtime will result in errors.

To see how these settings are used, add a Button control to the default Form1 (see Figure 2).

Double-click on the Button control to switch to its code-behind. For the Click event, code the following:

Figure 2. Add a Control: Adding a Button control to the form.

private void button1_Click(object sender, EventArgs e)
            ColorDialog cd = new ColorDialog();
            DialogResult result = cd.ShowDialog();
            if (result == DialogResult.OK)
                this.BackColor = cd.Color;
                Properties.Settings.Default.FormBackgroundColor = 
Essentially, when the user clicks on the button, it prompts a Color dialog window to let them choose a color (see Figure 3). After that, the background color of the form is changed to the selected color. At the same time, the selected color is also saved in the FormBackgroundColor setting. You access the settings through the Properties.Settings.Default class. Finally, remember to persist the application setting value using the Save() method.

When the form is resized, save its new size to the FormSize setting:

        private void Form1_Resize(object sender, EventArgs e)
            Properties.Settings.Default.FormSize = this.Size;
Finally, when the form is closed, save its new location to the FormPosition setting:

Figure 3. Display a Dialog: Displaying the Color dialog to allow users to select a color.

        private void Form1_FormClosed(
           object sender, FormClosedEventArgs e)
            Properties.Settings.Default.FormPosition = this.Location;
When the form is loaded, you will retrieve the values from the settings and assign them to their respective properties:

        private void Form1_Load(object sender, EventArgs e)
            this.BackColor = 
            this.Size = Properties.Settings.Default.FormSize;
            this.Location = Properties.Settings.Default.FormPosition;
You can now press F5 to test the application. Click on the Button control to change the background color of the form. Then resize it and move the form to a new location on the form. Click the close (“x”) button of the form to close the application. When you press F5 to run the application again, you will realize that the form will now display the color you have selected previously and it will also retain its previous size and location.

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