简体   繁体   English

将禁用的TextBox的ForeColor设置为与C#中的BackColor相同

[英]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#? 如何将禁用的TextBox的当前文本颜色设置为与C#中的当前背景颜色相同?

Simply doing txtLala.ForeColor = txtLala.BackColor does not seems to work. 简单地做txtLala.ForeColor = txtLala.BackColor似乎不起作用。

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 . 尝试在readonly之前设置color 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 ? 为什么不设置为ReadOnlyVisible = False

Maybe you have a good reason for Enabled = false 也许你有充分的理由认为Enabled = false

But you should note: 但你应该注意:

Use the Enabled property to specify or determine whether a control is functional. 使用Enabled属性指定或确定控件是否有效。 When set to false, the control appears dimmed , preventing any input from being entered in the control. 设置为false时,控件显示为灰色 ,防止在控件中输入任何输入。

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. 但是,调暗和锁定控件仅适用于Microsoft Internet Explorer版本4及更高版本。

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. 如果这是一个只读文本框,则需要先显式设置BackColor,然后您的语句才能生效。

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

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

Then again, if it's readonly, a label might be better. 再说一遍,如果它是re​​adonly,标签可能会更好。 If you're trying to hide it, perhaps setting .Visible = false would be better still. 如果你试图隐藏它,也许设置.Visible = false会更好。


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 . 关于winforms此站点建议将框放入框架并在框架上设置Enabled = false ,而不是文本框 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. 它似乎只适用于只读的TextBox。 If it is disabled (.Enabled = false). 如果禁用(.Enabled = false)。 It does not seems to work. 它似乎不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中更改DataGridViewRows的foreColor和BackColor - Change foreColor and BackColor of DataGridViewRows in C# 更改文本框中文本的前景色和背景色 - Changing the forecolor and backcolor of text in a textbox C# MetroTile 背景色/前景色在鼠标进入/离开期间不会改变 - C# MetroTile backcolor/forecolor not changing during mouse enter/leave c# tooltip backcolor 和 forecolor 不随使用方法或其他方式改变 - c# tooltip backcolor and forecolor are not changing with using method or other ways 可以在异步任务中更改前景色但不能更改文本框的背景色 - Can change forecolor but not backcolor of textbox in async task 更改其背景色时,将重置文本框的前色,反之亦然(使用PostBack) - The forecolor of textbox is reset when I change its backcolor, vice versa (with PostBack) C#Datetimepicker:将前色设置为特定日期 - C# Datetimepicker : Set forecolor to Specific Date 如何更改禁用的数字上调的前色/背景色? - How can I change the forecolor / backcolor of a disabled numeric updown? 如何根据输入到文本框的方式更改标签的原色和底色 - How to change the forecolor and backcolor of a label based on input into a textbox 使用DataGridViewButtonColumn C#winform设置背景色 - set backcolor using DataGridViewButtonColumn C# winform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM