2016-04-20 29 views
1

我做了一個新的自定義按鈕和一個新的布爾屬性。 當我在Visual Studio中將新的自定義按鈕添加到我的窗體時,設計功能很好。根據運行時新的自定義屬性初始化自定義c#控件

我想加載兩個設計取決於該布爾屬性,但父窗體已經在formdesigner.cs中的設計代碼和按鈕看起來是一樣的。

我應該重寫什麼方法來訪問按鈕的「加載」?

這是我的按鈕CS

 namespace Regio.UI 
     { 
public class MetroButton : System.Windows.Forms.Button 
{ 
    public bool Highlight { get; set; } 
    public MetroButton() 
    { 
     if (Highlight == true) 
     { 


      base.BackColor = Color.White; 
      base.ForeColor = Color.Black; 
      FlatStyle = FlatStyle.Flat; 
      FlatAppearance.BorderColor = Color.Gray; 
      FlatAppearance.BorderSize = 2; 
      FlatAppearance.MouseOverBackColor = Color.WhiteSmoke; 
     } 
     else { 
      base.BackColor = Color.White; 
      base.ForeColor = Color.Black; 
      base.FlatStyle = FlatStyle.Flat; 
      base.FlatAppearance.BorderColor = Color.FromArgb(0, 174, 219); 
      base.FlatAppearance.BorderSize = 3; 
      base.FlatAppearance.MouseOverBackColor = Color.WhiteSmoke; 

     } 
    } 

} 
} 

這是在Form1.designer.cs:提前

this.metroButton1 = new Regio.UI.MetroButton(); 
     this.metroButton2 = new Regio.UI.MetroButton(); 
     this.SuspendLayout(); 
     // 
     // metroButton1 
     // 
     this.metroButton1.BackColor = System.Drawing.Color.Black; 
     this.metroButton1.FlatAppearance.BorderColor = System.Drawing.Color.Gray; 
     this.metroButton1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 
     this.metroButton1.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
     this.metroButton1.ForeColor = System.Drawing.Color.White; 
     this.metroButton1.Highlight = false; 
     this.metroButton1.Location = new System.Drawing.Point(212, 172); 
     this.metroButton1.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0); 
     this.metroButton1.MinimumSize = new System.Drawing.Size(100, 30); 
     this.metroButton1.Name = "metroButton1"; 
     this.metroButton1.Padding = new System.Windows.Forms.Padding(3); 
     this.metroButton1.Size = new System.Drawing.Size(171, 111); 
     this.metroButton1.TabIndex = 0; 
     this.metroButton1.Text = "metroButton1"; 
     this.metroButton1.UseVisualStyleBackColor = false; 

感謝

回答

3

呀,Visual Studio可以是惱人放肆的時候它涉及到自定義控件。但是你的代碼確實有缺陷。你已經把你的構造函數放在布爾值爲真的時候,它被設計成一種方式,當它被設計爲假時,它被設計爲另一種方式。不過,我想你會希望設計在布爾變化時重置。以下是我將如何改變這一點。

public class MetroButton : System.Windows.Forms.Button 
{ 
    private bool highlight; 
    public bool Highlight 
    { 
     get 
     { 
      return highlight; 
     } 
     set 
     { 
      highlight = value; 
      if (highlight)//This is the same as "if(highlight == true)" but the last part is redundant in most programming languages. 
      { 

       FlatAppearance.BorderColor = Color.Gray; 
       FlatAppearance.BorderSize = 2; 
       FlatAppearance.MouseOverBackColor = Color.WhiteSmoke; 
      } 
      else 
      { 
       base.FlatAppearance.BorderColor = Color.FromArgb(0, 174, 219); 
       base.FlatAppearance.BorderSize = 3; 
       base.FlatAppearance.MouseOverBackColor = Color.WhiteSmoke; 
      } 
     } 
    } 
    public MetroButton() 
    { 
     base.BackColor = Color.White; 
     base.ForeColor = Color.Black; 
     FlatStyle = FlatStyle.Flat; 
     Highlight = true;//Just setting a default. 
    } 

} 

現在,無論何時改變「Highlight」的值,if語句中的代碼都會運行。

+0

工作得很好,謝謝:) – TrulyXax

+0

不客氣,任何幫助^ _ ^ – Rariolu