Sheetal
Sheetal

Reputation: 873

Retaining the changes made to the backcolor property

I am developing a windows application.

I have 3 forms:

I want to change the backcolor of all the 3 forms to the color selected by the user.

I have used the following code I am able to change the backcolor but When I exit the application and restart it I am not able to get the color that user has set. I am getting the default colour only.

Is it possible to retain the colour selected by the user and use it as backcolor when the user restarts the application.

CODE

In Form1

ColorDialog c1 = new ColorDialog();

   public static System.Drawing.Color bkc;
    private void button1_Click(object sender, EventArgs e)
    {

        DialogResult res = c1.ShowDialog();
        if (res == DialogResult.OK)
        {
            bkc = c1.Color;
            this.BackColor = bkc;


            MessageBox.Show(Convert.ToString(bkc));
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 obj1 = new Form2();
        obj1.BackColor = bkc;
        obj1.Show();
    }

In Form 2 CODE

    private void button2_Click(object sender, EventArgs e)
    {
        Form3 obj1 = new Form3();
        obj1.Show();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        this.BackColor = Form1.bkc;

    }

In Form3 CODE

    private void button2_Click(object sender, EventArgs e)
    {
        Form1 obj1 = new Form1();
        obj1.Show();
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        //Form1 obj2 = new Form1();
        this.BackColor = Form1.bkc;
    }

In the color dialog box I am selecting a color and pressing Ok button the color is also changed but when I restart the application I dont get the colour which I set using the Color Dialog.I want to retain this setting so that the user can get the desired color without resetting it each time the application is executed.

The above code does not generate any error.

can anybody help me out in performing this task?

Thanks in advance!

Upvotes: 0

Views: 366

Answers (4)

Robert Rossney
Robert Rossney

Reputation: 96712

The suggestion of using the application configuration file is close, but there are two things wrong with it.

First, all users of the application share the same application configuration file. If you have multiple users (on a network, say, or different users on the same machine), storing a user's preference in the application configuration file will change that setting for all users. A second thing wrong with it is that under a default installation on Vista it won't work anyway: by default, Vista doesn't give the user write access to anything under the Program Files directory, so saving changes to the application configuration file will throw an exception.

The right answer is to use user settings. These get stored in the application's user settings file, which lives in a (deeply nested, and OS-version-dependent) subdirectory of the user's home directory. The ConfigurationManager loads these settings at runtime, and lets you update and save them in your code. There's an entire infrastructure built into Visual Studio to make this (relatively) easy, which is good, because doing it properly involves writing a spooky amount of code against the ConfigurationManager class. Here's how it works:

If you look under the Properties of your VS project, you'll see an item called Settings.settings. When you double-click on this, it will show you a grid that lets you add settings to your project. You give the setting name, choose its the data type and default value, and, crucially, the scope. The setting can be application scope, in which case its value will be common to all users of the application and be stored in the application configuration file. Or it can be user scope, in which case each user can have his own value for the setting, and the setting will live in the user settings file.

When you add a setting to this grid, VS generates code to make the setting available to your code. Basically, it creates a class that exposes these settings to your code as properties of a singleton object. (You can see this code if you want to get an idea of what this is saving you from having to do yourself; it gets stored in the 'Settings.Designer.cs' file created under 'Settings.settings' in the project view.) It also, conveniently, regenerates this class every time you change the information in the Settings grid. Once you create a setting in the settings grid, you can reference it in your code thusly:

ctl.BackColor = Properties.Settings.Default.BackColor;

User settings can be modified by your code:

Properties.Settings.Default.BackColor = newBackColor;

And you can save them to the user settings file like this:

Properties.Settings.Default.Save();

Having these settings being exposed as properties of a class is useful for a lot of reasons. One of the most important is that since they're properties (and not, say, dictionary entries accessed by a key, which is how most code that people write against the ConfigurationManager class works), there's compile-time checking of the names you're using in code. You're not ever going to get a NullReferenceException at runtime if you misspell the name of a setting; you'll get an error when you compile it instead.

There are a few subtleties to using user settings. One of the less obvious ones is: what happens when you produce a new release of the software? The user settings are stored in a directory that's keyed to the version number of the program; if you release a new version, the user settings file for it won't exist. How do you keep the user from losing all of his settings when he upgrades your program?

This is also built in to that Settings class; all you need to do is this:

if (Properties.Settings.Default.UpgradeSettings)
{
   Properties.Settings.Default.Upgrade();
   Properties.Settings.Default.UpgradeSettings = false;
}

This will copy the user's settings from the previous release into the settings file for the new release.

Upvotes: 1

Madeleine
Madeleine

Reputation: 2182

Why dont you create an event that all three forms listen to and get them to change the background colour when listening to the "change colour" event? And you could store the colour in a static variable so that when the form gets loaded, the background colour could be set to that stored in the variable.

In order for the screen to remember the colour settings, why not store the colour selected in a user preferences file? Try the "IsolatedStorage" functionality to save a preferences file.

Upvotes: 0

Mitch Wheat
Mitch Wheat

Reputation: 300549

You will need to save the value somewhere such as the Application.exe.config:

    // Open App.Config of executable
    System.Configuration.Configuration config =
     ConfigurationManager.OpenExeConfiguration
                (ConfigurationUserLevel.None);

    // Add an Application Setting.
    config.AppSettings.Settings.Add("BackgroundColour",
                   bkc + " ");

    // Save the changes in App.config file.
    config.Save(ConfigurationSaveMode.Modified);

    // Force a reload of a changed section.
    ConfigurationManager.RefreshSection("appSettings");

Here is a C# full code example: Using System.Configuration.ConfigurationManager Example

Upvotes: 3

shahkalpesh
shahkalpesh

Reputation: 33476

You are doing it wrong way.

How will the application remember the user choice of backcolor?
The app runs in memory & shows chosen backcolor till its terminated.

Read on this & take it forward.

EDIT: Also, it is not right thing to use Form1.BackColor in Form2.
Open Form1, change backcolor, close Form1 & open Form2 to see what happens (you might see that Form1 opens again).

Upvotes: -1

Related Questions