Why am I getting “Control does not support transparent background colors”?

后端 未结 5 1693
北海茫月
北海茫月 2020-12-11 23:24

I am working on a C# - Winforms application and attempting to set the background colour of a read-only text box like so...

txtMyBox.BackColor = Color.FromNam         


        
相关标签:
5条回答
  • 2020-12-11 23:37

    A better way would be:

    txtMyBox.BackColor = Color.Red;
    

    The error you getting is being caused because of somewhere else in your code where you are trying to set the background color of the form itself to be transparent and that is not supported, check your code carefully and you will find somthing like that:

    BackColor = Color.Transparent;
    

    Since there is no element name (i.e. myControl.BackColor = somthing) and your sentence start with BackColor = somthing - the compiler thinks you want to change the background color of the windows form into being transparent, check your form editor too - you might doing that from there.

    Here is a reproduction of your error:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BackColor = Color.Transparent; //Control does not support transparent background colors.
        }
    }
    
    0 讨论(0)
  • 2020-12-11 23:42

    Quite old post but... Have you tried this before?

    public partial class MyForm : Form
    {
        public MyForm()
        {
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    
            InitializeComponent();
        }
    }
    
    0 讨论(0)
  • 2020-12-11 23:42
    ColorTextBox.BackColor = colorDialog1.Color;
    textBox2.BackColor = System.Drawing.Color.FromArgb(
                         ColorTextBox.BackColor.ToArgb()); 
    
    0 讨论(0)
  • 2020-12-11 23:42

    A bit late - but eventually helps this someone who - like me - found this page according to the OPs question:

    I got this error when Setting a Winforms Splitter background color which was generated by a

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

    The solution was to generate the Color value instead with the following helper method:

    ColorTranslator.FromHtml("#C9D9EB") 
    

    This avoids generating a transparency Information.

    0 讨论(0)
  • 2020-12-11 23:44

    Try this instead of FromName

     txtMyBox.BackColor = Color.Red;
    
    0 讨论(0)
提交回复
热议问题