5

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.FromName ("Red");

This is failing with the error message...

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

First things first; is this the right way to set the background colour for a read-only textbox? I'm doing this a lot for ordinary textboxes and it appears to work fine.

If it is, could someone help me out with what a "transparent background color" is, and why I should want one? I don't think I do; I just want the background to change colour.

Brian Hooper
  • 21,544
  • 24
  • 88
  • 139

5 Answers5

10

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

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

        InitializeComponent();
    }
}
Luiz Duarte
  • 131
  • 2
  • 3
3

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.

John Ranger
  • 541
  • 5
  • 18
2

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.
    }
}
G.Y
  • 6,042
  • 2
  • 37
  • 54
  • Thanks for the tip, but I can't find the word "transparent" in any file associated with the solution. – Brian Hooper May 14 '12 at 15:54
  • It might exist as Color from ARGB(0,255,255,255). check carefully - i promise it is there somewhere. another way is trying to set Backcolor = 0 – G.Y May 14 '12 at 16:16
  • Righto. I'll have another try tomorrow. Thanks again for your help. – Brian Hooper May 14 '12 at 17:40
  • 1
    You were right; it was well-hidden but I finally tracked it down (`BackColor = Color.FromColor("Some Colour that Doesn't Exist")`). Thanks again. – Brian Hooper May 15 '12 at 07:43
0

Try this instead of FromName

 txtMyBox.BackColor = Color.Red;
Nick Sinas
  • 2,594
  • 11
  • 42
  • 51
0
ColorTextBox.BackColor = colorDialog1.Color;
textBox2.BackColor = System.Drawing.Color.FromArgb(
                     ColorTextBox.BackColor.ToArgb()); 
David
  • 15,894
  • 22
  • 55
  • 66