简体   繁体   中英

How do I set a color as a hexadecimal variable?

I want to make one of those cool little color clocks for myself, that sets the color to a time based variable.

I made the variable "One" as being equal to the time as it's six basic characters, but...

System.Drawing.Color time = System.Drawing.ColorTranslator.FromHtml("#" + One);

^ That gives me the error that "A field initializer cannot reference the non-static field, method, or property 'TimeColor_Clock.TCC.One'"

How can I get my code to accept my variable? I've tried a few different methods (of which none have worked with the variable) but this really seems like the most direct one.

It's because you're doing this outside of a method. Do it instead in your constructor

System.Drawing.Color time;

public MyClass()
{
    time = System.Drawing.ColorTranslator.FromHtml("#" + One);
}

or a method

public void UpdateColor()
{
    time = System.Drawing.ColorTranslator.FromHtml("#" + One);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM