简体   繁体   中英

Set a disabled TextBox's ForeColor to be the same as its BackColor in C#

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.

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. , preventing any input from being entered in the control. ,防止输入任何输入。

Note The ability to enable or disable functionality is always available.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM