简体   繁体   English

Winform-自定义文本框在只读时更改背景色

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM