C# .NET - Very simple question - Asked By Vlince on 20-Jul-14 02:20 PM

I've created a simple winform that has a label1 and a button1. By default, the label1 has a background color of "red" when I click the button1 I want the label1's bacground color to change from "red" to "green"

simple right :) !!!

When I try:

.
.
.
private void button1_Click(object sender, System.EventArgs e)
{
label1.BackColor = "green";
button1.Text = "test"; 
}
.
.
.
When I execute I get this error message:
C:\BegCSharp\Chapter10\WindowsApplication\Form1.cs(174): Cannot implicitly convert type 'string' to 'System.Drawing.Color'

Now when I double click the error message it bring me to the "green" string. If I comment the :
//label1.BackColor = "green";
line, then everything works great.

Reading the error message made me realized that it cannot *implicitly* convert type 'string'
So then decided that "ok, I'll define a variable as string this way I'm not *implicitly* creating a type 'string', I'm Explicitly creating one" so I've changed the code to this:

private void button1_Click(object sender, System.EventArgs e)
{
string MyColor = "green";
label3.BackColor = MyColor;
button1.Text = "test"; 
}

Still get the same error, now perhaps I'm missing something since this approach does/should work no ???

This is not a string - Asked By Peter Bromberg on 15-Dec-01 07:56 AM

Look up System.Drawing.Color in WINCV:
  public virtual bool Equals(object obj);
    public static System.Drawing.Color FromArgb(int alpha, System.Drawing.Color baseColor);
    public static System.Drawing.Color FromArgb(int alpha, int red, int green, int blue);
    public static System.Drawing.Color FromArgb(int argb);
    public static System.Drawing.Color FromArgb(int red, int green, int blue);
    public static System.Drawing.Color FromKnownColor(System.Drawing.KnownColor color);
    public static System.Drawing.Color FromName(string name);
    public float GetBrightness();
    public virtual int GetHashCode();
    public float GetHue();
    public float GetSaturation();
    public Type GetType();
    public int ToArgb();
    public System.Drawing.KnownColor ToKnownColor();
    public virtual string ToString();
=======================================================
You need to use one of the overriden  conversion methods 
listed. e.g. , " FromArgb(int alpha, int red, int green, int blue);"

Using Color.Green - Asked By XIDONG ZHANG on 25-Jan-02 09:36 AM

Many colors pre-defined in class Color. you can get these colors by using Color.Green, Color.Red, etc.

See This - SP replied to Vlince on 07-Jun-08 07:37 AM


you need to add following namespace to your page.

using System.Drawing;

And use the following line of code to change the backcolor of a label on button click.

lblname.BackColor = System.Drawing.Color.Green;


very simple question - arkar myint replied to Vlince on 20-Jul-14 02:21 PM
 i suggest you that are below

private void button1_Click(object sender, EventArgs e)
{
label1.BackColor=Color.green;
button1.Text="test";
}
zhang replied to Vlince on 02-Sep-10 05:28 AM
I like this kind of very useful articles
zhang replied to Vlince on 02-Sep-10 05:34 AM
This is one of the best posts that I’ve ever seen; you may include some more<a rel="follow" href="http://www.cncwm.com/" rel="nofollow">crystal block</a>ideas in the same theme. I’m still waiting for some interesting thoughts from your side in your next post.
zhang replied to Vlince on 03-Sep-10 03:51 AM
I am totally agree with your oppinion.this blog post is very http://www.hzhwknit.com/ encouraging to people who want to know these topics.