[英]Winform - Custom TextBox change backcolor when Readonly
Hi I have a custom TextEditor
: 嗨,我有一个自定义
TextEditor
:
public partial class TextEditor : TextBox
{
public TextEditor() : base()
{
this.Font = new Font("Calibri", 12.0f);
this.BackColor = Color.Gainsboro;
this.BorderStyle = BorderStyle.FixedSingle;
if (this.ReadOnly)
{
this.BackColor = Color.DarkGray;
}
}
protected override void InitLayout()
{
base.InitLayout();
base.CharacterCasing = _charCasing;
//SetStyle(ControlStyles.UserPaint, true);
}
}
I would like to change its BackGroundColor
when the property ReadOnly = true
but its not working. 我想在属性
ReadOnly = true
但BackGroundColor
时更改其BackGroundColor
。
Any clue?
有什么线索吗?
You are doing it on constructor. 您正在构造函数上执行此操作。 Which
ReadOnly
be default to False
其中
ReadOnly
默认为False
What you need is listen to ReadOnlyChanged
event 您需要的是监听
ReadOnlyChanged
事件
public partial class TextEditor : TextBox
{
public TextEditor()
{
this.Font = new Font("Calibri", 12.0f);
this.BackColor = Color.Gainsboro;
this.BorderStyle = BorderStyle.FixedSingle;
ReadOnlyChanged += OnReadOnlyChanged;
}
private void OnReadOnlyChanged(object sender, EventArgs eventArgs)
{
if (ReadOnly)
BackColor = Color.DarkGray;
}
protected override void InitLayout()
{
base.InitLayout();
CharacterCasing = _charCasing;
//SetStyle(ControlStyles.UserPaint, true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.