[英]Winform - Custom TextBox change backcolor when Readonly
嗨,我有一個自定義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);
}
}
我想在屬性ReadOnly = true
但BackGroundColor
時更改其BackGroundColor
。
有什么線索嗎?
您正在構造函數上執行此操作。 其中ReadOnly
默認為False
您需要的是監聽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.