繁体   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