Set form background color

I've created key in registry where background form color is saved (in format: ff8f8f8f, and string key is called myKey). The code which saves color in registry is:

if(cd1.ShowDialog()==DialogResult.OK)
{
string boja= cd1.Color.Name;
RegistryKey regkey;
regkey=Registry.LocalMachine.OpenSubKey(@"Software\myProject\1\options",true);

regkey.SetValue("myKey",boja);
regkey.Close();
}

What I want is, when I start my Form1 to BackColor be set to myKey value.
Can you help me!

Thanks



Answer this question

Set form background color

  • Sandeep_MSFT

    When I write that, error message Cannot implicitly convert type 'object' to 'string' is shown Can you, please, fix that
  • Dovik

    Sorry, my bad. Didn't tested anything, just wrote it out of the head. You need to cast the object return type to a string:


    private void Form1_Load(object sender, EventArgs e)
    {
    RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"Software\myProject\1\options",true);
    string colorName = (string)regkey.GetValue( "myKey", BackColor.Name );

    BackColor = Color.FromName( colorName );
    }




  • C.R.B.


    private void Form1_Load(object sender, EventArgs e)
    {
    RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"Software\myProject\1\options",true);
    this.BackColor = regkey.GetValue( "myKey", BackColor );
    }



  • Don 007

    Thanks!!
  • Set form background color