文本框只读时如何更改文本的前景色 [英] How to change forecolor of text when textbox is readonly
本文介绍了文本框只读时如何更改文本的前景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
美好的一天
我自己制作了文本框,我想更改文本框内文本的前景色。
我编写了以下代码,但它没有将文本框内文本的forecolor更改为灰色。
Good day
I made my textboxes reaadonly and I want to change the forecolor of the text inside of the textboxes.
I have written the following code, but it is not changing the forecolor of the text inside of the textbox to grey.
txtVersion.ForeColor = Color.Gray;
如果文本框是只读的,我该如何更改文本的前景色? ?
Thanx
How can I change the forecolor of the text, when the textbox is made readonly?
Thanx
推荐答案
只读系统的ForeColor属性有些奇怪.Windows .Form.TextBox我还没有看到记录,虽然我看起来不是很努力。
我注意到前景颜色改变只有在生效之后才生效背景颜色已更改一次。之后,任何后续的前景变化都会立即发生。
我的小测试应用程序:
There is something odd about the ForeColor property of a read only System.Windows.Form.TextBox which I haven't seen documented, although I haven't looked very hard.
I've noticed that a foreground colour change only takes effect after the background colour has been changed once. After that any subsequent foreground changes take place immediately.
My little test app:
using System;
using System.Drawing;
using System.Windows.Forms;
// Requires a form with 2 text boxes and 2 buttons
public partial class Form1 : Form {
private readonly Color[] rainbow = new Color[] {
Color.Red,
Color.Orange,
Color.Yellow,
Color.Green,
Color.Blue,
Color.Indigo,
Color.Violet
};
private Int32 foreIdx, backIdx;
public Form1() {
InitializeComponent();
backIdx = rainbow.Length - 1;
NormalTextBox.Text = "Standard text box";
ReadOnlyTextBox.Text = "Read only text box";
}
private void ForeColBtn_Click(object sender, EventArgs e) {
NormalTextBox.ForeColor = rainbow[foreIdx];
ReadOnlyTextBox.ForeColor = rainbow[foreIdx];
foreIdx = ++foreIdx % rainbow.Length;
}
private void BackColBtn_Click(object sender, EventArgs e) {
NormalTextBox.BackColor = rainbow[backIdx];
ReadOnlyTextBox.BackColor = rainbow[backIdx];
backIdx = ++backIdx % rainbow.Length;
}
}
玩得开心,
Alan。
Have fun,
Alan.
你不能,没有大量工作。
当你将文本框设置为只读时,前景和背景颜色被覆盖并被忽略,以支持系统禁用的颜色 - BAAAAD改变它们的想法。
最简单的方法是是创建自己的控件,从TextBox派生并自己处理ReadOnly - 通过KeyPress事件禁用文本输入并自己设置颜色。
You can't, without a lot of work.
When you set the textbox to readonly, the foreground and background colours are overridden and ignored in favour of the system disabled colors - and it is a BAAAAD idea to change them.
The easiest way to do it would be to create your own control, derived from TextBox and handle ReadOnly yourself - disable text entry via the KeyPress event and set the color yourself.
我所做的是,而不是使用Readonly,我使用Enabled。
What I have done is, instead of using Readonly , I use Enabled.
这篇关于文本框只读时如何更改文本的前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文