5

How can I change and save the back color in a C# Windows Application so that when I close the application and run the program again the new color will be the back color default?

Servy
  • 202,030
  • 26
  • 332
  • 449
Kamal Mahdavi
  • 39
  • 1
  • 5
  • Use a user setting http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx (old article but still applies) – lc. Sep 28 '12 at 16:18

4 Answers4

5

You can get that going with very little effort. Select the form in the designer, in the Properties window open the ApplicationSettings node. Select (PropertyBinding) and click the button. Select BackColor in the popup dialog. Click the dropdown arrow and click New. Set the name to, say, "FormBackColor".

The only other thing you need is an option to let the user pick another color. Very easy to do with the ColorDialog class:

    private void OptionChangeColor_Click(object sender, EventArgs e) {
        using (var dlg = new ColorDialog()) {
            if (dlg.ShowDialog() == DialogResult.OK) {
                this.BackColor = Properties.Settings.Default.FormBackColor = dlg.Color;
                Properties.Settings.Default.Save();
            }
        }
    }

enter image description here

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

You'll need to save the new color in some file that you load on startup and apply as the background color.

Or use a user setting like this.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
1

You could do something simple like File.WriteAllText("bg.txt", this.BackColor.ToString()); and when the app loads do this.BackColor = Color.FromName(File.ReadAllText("bg.txt"));

Of course storing this color in Isolated Storage or in the registry might be better. But you get the idea...

Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
0

Some time ago there was thread about best practices to do that here on stackoverflow.
Please take look:
Best practice to save application settings in a Windows Forms Application

Community
  • 1
  • 1
d3r0n
  • 128
  • 10