tags:

views:

2382

answers:

3

How do I set a disabled TextBox's current text color to be the same as its current background color in C#?

Simply doing txtLala.ForeColor = txtLala.BackColor does not seems to work.

A: 

If this is a readonly textbox, you need to explicitly set your BackColor first, then your statement will work.

txtLala.BackColor = System.Drawing.SystemColors.Info;
txtLala.ForeColor = txtLala.BackColor;

Ref: http://bytes.com/groups/net-c/233961-read-only-textbox

Then again, if it's readonly, a label might be better. If you're trying to hide it, perhaps setting .Visible = false would be better still.


Edit: This seems to be a common question on the web. With respect to winforms: This site suggests dropping the box into a frame and setting Enabled = false on the frame, not the textbox. Once you do that, you may be able to maintain control of the forecolor.

Michael Haren
+3  A: 

This works:

txtLala.Text = "Red";
txtLala.BackColor = System.Drawing.Color.Red;
txtLala.ForeColor = txtLala.BackColor;
txtLala.ReadOnly = true;

Try setting the color, before the readonly. And also check how you are setting the color!

EDIT

Try this

txtLala.Attributes.Add("style","background-color:Red;color:Red");

If you are trying to make it invisible, you know you can set it as

txtLala.Visible = False;

EDIT II

I finally tried

txtLala.Enabled = false;

... you see that grey shadow color! I don't think you can mess with that, it looks to be a browser property setting.

Why not set as ReadOnly or Visible = False?

Maybe you have a good reason for Enabled = false

But you should note:

Use the Enabled property to specify or determine whether a control is functional. When set to false, the control appears dimmed, preventing any input from being entered in the control.

Note The ability to enable or disable functionality is always available. However, dimming and locking the control only works in Microsoft Internet Explorer version 4 and later.

This property propagates down the control hierarchy. Therefore, disabling a container control will disable all child controls within that container.

Note Not all controls support this property. See the indivual controls for details.

DrG
+1 complete answer
Daok
A: 

It seems to only work for TextBox that is read only. If it is disabled (.Enabled = false). It does not seems to work.

Have you tried it with CSS styling, this might do the trick.
Drejc
Agreed, it would work with css
DrG
I don't think this is ASP...
Michael Haren
it is c#, yes, what he says is true.
DrG
C# could mean ASP.NET or Winforms... the difference is important
Michael Haren
Ah ok, I'm leaning to Winforms too.
DrG