Why do I get "Control does not support transparent background colors"?

I am working on a C # - Winforms application and trying to set the background color of a read-only text field ...

txtMyBox.BackColor = Color.FromName ("Red");

This is an error message with an error message ...

System.ArgumentException was unhandled
Message=Control does not support transparent background colors.
Source=System.Windows.Forms

First things first; is this the correct way to set the background color for a read-only text field? I do this a lot for regular text fields and it seems to work fine.

If so, can someone help me with what “transparent background color” is and why should I want it? I do not think I know; I just want the background to change color.

+3
source share
5 answers

The best way:

txtMyBox.BackColor = Color.Red;

, , - , - , , , , , -

BackColor = Color.Transparent;

(, myControl.BackColor = somthing), BackColor = somthing - , , - .

:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        BackColor = Color.Transparent; //Control does not support transparent background colors.
    }
}
+2

, ... ?

public partial class MyForm : Form
{
    public MyForm()
    {
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

        InitializeComponent();
    }
}
+2

FromName

 txtMyBox.BackColor = Color.Red;
0
ColorTextBox.BackColor = colorDialog1.Color;
textBox2.BackColor = System.Drawing.Color.FromArgb(
                     ColorTextBox.BackColor.ToArgb()); 
0

, , , , OPs:

I got this error when setting the background color of the Splitter Winforms that was generated

Color.FromArgb(0xC9,0xD9,0xEB);

The solution was to generate a color value instead of the following helper method:

ColorTranslator.FromHtml("#C9D9EB") 

This avoids creating transparency information.

0
source

All Articles