Export (0) Print
Expand All

How to: Create Application Settings


In the following procedure, you manually create a wrapper class that derives from ApplicationSettingsBase. To this class you add a publicly accessible property for each application setting that you want to expose.

You can also perform this procedure using minimal code in the Visual Studio designer. Also see How to: Create Application Settings Using the Designer.

To create new Application Settings programmatically

  1. Add a new class to your project, and rename it. For this procedure, we will call this class MyUserSettings. Change the class definition so that the class derives from ApplicationSettingsBase.

  2. Define a property on this wrapper class for each application setting you require, and apply that property with either the ApplicationScopedSettingAttribute or UserScopedSettingAttribute, depending on the scope of the setting. For more information about settings scope, see Application Settings Overview. By now, your code should look like this:

    using System;
    using System.Configuration;
    using System.Drawing;
    public class MyUserSettings : ApplicationSettingsBase
        public Color BackgroundColor
                return ((Color)this["BackgroundColor"]);
                this["BackgroundColor"] = (Color)value;
  3. Create an instance of this wrapper class in your application. It will commonly be a private member of the main form. Now that you have defined your class, you need to bind it to a property; in this case, the BackColor property of your form. You can accomplish this in your form's Load event handler.

    MyUserSettings mus;
    private void Form1_Load(object sender, EventArgs e)
        mus = new MyUserSettings();
        mus.BackgroundColor = Color.AliceBlue;
        this.DataBindings.Add(new Binding("BackColor", mus, "BackgroundColor"));
  4. If you provide a way to change settings at run time, you will need to save the user's current settings to disk when your form closes, or else these changes will be lost.

    	//Make sure to hook up this event handler in the constructor! 
    	//this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); 
            void Form1_FormClosing(object sender, FormClosingEventArgs e)



© 2015 Microsoft