簡體   English   中英

Winform-自定義文本框在只讀時更改背景色

[英]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 = trueBackGroundColor時更改其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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM