2016-09-14 39 views
0

您好我有一個自定義TextEditor自定義文本框變化背景色:的Winform時只讀

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

任何線索?

+0

你可以試試這個,讓我知道結果如何:'this.BackColor = System.Drawing.SystemColors.GrayText' 。 –

+0

嘗試將if(this,ReadOnly)更改爲if(this.ReadOnly == true) – active92

回答

0

你正在構造函數上做。這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); 
    } 
}