I A Khan
I A Khan

Reputation: 8841

How to Change Form background color in C#

I am using following code in form_load event to change Form.BackgroundColor, but it is giving me an error.

Control does not support transparent background colors.

Here is what I am trying...

private void Form1_Load(object sender, EventArgs e)
{
  string sColor = "#ACE1AF";// Hex value of any color
  Int32 iColorInt = Convert.ToInt32(sColor.Substring(1), 16);
  Color curveColor = System.Drawing.Color.FromArgb(iColorInt);
  this.BackColor = curveColor;
 }

I found the same question (Why am I getting "Control does not support transparent background colors"?), but is not met my requirement due to in this Color class is using its default values.

Upvotes: 0

Views: 2419

Answers (2)

isxaker
isxaker

Reputation: 9446

ColorTranslator.FromHtml("#00FEF2D4");

Edit : ColorTranslator.FromHtml Translates an HTML color representation to a GDI+ Color structure.

Parameters

htmlColor

Type: System.String

The string representation of the Html color to translate.

Return Value

Type: System.Drawing.Color

The Color structure that represents the translated HTML color or Empty if htmlColor is null.

Upvotes: 2

Patrick Hofman
Patrick Hofman

Reputation: 156918

The problem is, you are trying to make the background transparent. The color you specify in ARGB is 100% transparent. Hence the error.

You should use:

void Form1_Load(object sender, EventArgs e)
{
    string sColor = "#FFACE1AF";// Hex value of any color
    Int32 iColorInt = Convert.ToInt32(sColor.Substring(1), 16);
    Color curveColor = System.Drawing.Color.FromArgb(iColorInt);
    this.BackColor = curveColor;
}

Setting the alpha channel to FF.

Upvotes: 2

Related Questions