Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to store last used color from the color dialog box and load it while i open the project again.

What I have tried:

I can store the combobox values on form closing like this.

Debugger.Properties.Settings.Default.mode = comboBox6.Text;
Debugger.Properties.Settings.Default.port1 = comboBox1.Text;
Debugger.Properties.Settings.Default.port2 = comboBox2.Text;
Debugger.Properties.Settings.Default.baud = comboBox3.Text;

and load it again like this.

comboBox6.Text=Debugger.Properties.Settings.Default.mode;
comboBox1.Text=Debugger.Properties.Settings.Default.port1;
comboBox2.Text=Debugger.Properties.Settings.Default.port2;
comboBox3.Text=Debugger.Properties.Settings.Default.baud;

But i can't able to store and retrieve the color. please help me on this.
Posted
Updated 3-May-16 2:39am
Comments
BillWoodruff 3-May-16 8:02am    
Important for this type of question to know exactly what 'Debugger is ... just in case it is not the obvious.

In Visual Studio:

1. open the Solution Explorer
2. under the Properties tab
3. double-click Settings

define a Setting there, select type 'User, select the type 'Color from the dropdown.

assume you created a Color Setting named 'MainFormBackColor.

I. to set that property:
C#
private void Form1_Load(object sender, EventArgs e)
{
    Properties.Settings.Default.MainFormBackColor = this.BackColor;
}
2. to change the value of that setting using a ColorDialog:
C#
private void button1_Click(object sender, EventArgs e)
{
    Color currentSavedColor = (Color) Properties.Settings.Default.MainFormBackColor;
    Color selectedColor;

    // get the dialog's initial color set to your current saved value
    colorDialog1.Color = currentSavedColor;

    if (colorDialog1.ShowDialog(this) == DialogResult.OK)
    {
        // user selected color

        selectedColor = colorDialog1.Color;

        // user wants to use new color ?
        if(MessageBox.Show(string.Format("Change current MainFormBackgroundColor?\r\nfrom: {0}\r\nto: {1}", currentSavedColor, selectedColor), "Color Change", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            this.BackColor = selectedColor;
            Properties.Settings.Default.MainFormBackColor = selectedColor;
        }
    }
}
Note:

1. back when, we used to have use this syntax: Properties.Settings.Default["MainFormBackColor"], but now we can use the syntax shown here (I believe the new syntax was introduced with .NET 3.5). But, the old syntax still works.

And, now go and study what happens if you make the Setting you created 'Application in scope, rather than 'User. Then, examine using the 'config file to persist settings, and, then, read articles here on CodeProject about strategies for saving big bunches of state information by serializing and de-serializing.
 
Share this answer
 
Try this

// Set the Color
C#
string colorName = Debugger.Properties.Settings.Default.Color;
         Color color = System.Drawing.Color.FromName(colorName);
         colorDialog1.Color = color;
         colorDialog1.ShowDialog();


// Get the Color
C#
Debugger.Properties.Settings.Default.Color = colorDialog1.Color.Name;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900