2012-09-17 55 views
2

我遇到了一個小問題,其中定義的自定義屬性值沒有粘在繼承的表單中。C#使用自定義屬性創建一個基本表單

在我的基本形式的代碼是:

namespace ContractManagement.Forms 
{ 
    public partial class BaseForm : Form 
    { 
     public BaseForm() 
     { 
      InitializeComponent(); 
     } 

     public Boolean DialogForm 
     { 
      get 
      { 
       return TitleLabel.Visible; 
      } 
      set 
      { 
       TitleLabel.Visible = value; 
       CommandPanel.Visible = value; 
      } 
     } 

     protected override void OnTextChanged(EventArgs e) 
     { 
      base.OnTextChanged(e); 
      TitleLabel.Text = Text; 
     } 
    } 
} 

然後在形式繼承此,我有:

namespace ContractManagement.Forms 
{ 
    public partial class MainForm : Forms.BaseForm 
    { 
     public MainForm() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

出於某種原因,儘管我在MainForm中設置DialogForm,上運行時將恢復爲True。

在這個網站上有另一篇文章提到了這一點,但我沒有得到它解釋。

我也想創建一個屬性,允許我隱藏ControlBox,那麼如何添加它呢?

+0

在某此鏈接 http://stackoverflow.com/questions/6872849/custom-properties-defined-in-base-form的看看-lose-their-state-in-inherited-form-upon-r – MethodMan

+0

@DJKRAZE - 這是我提到的另一篇文章,但我不明白剛剛完成上述操作時出了什麼問題。與其他文章中的答案相比,我需要做的與我的代碼有所不同,因爲我有兩件事正在嘗試設置。 – hshah

+0

對於初學者你想要存儲哪些屬性和/或值,或者可以訪問......在Initialize通知後的鏈接中,鏈接如何初始化然後_some控制名稱..例如 – MethodMan

回答

2

我相信我現在已經做到了:

namespace ContractManagement.Forms 
    { 
     public partial class BaseForm : Form 
     { 
      private Boolean DialogStyle; 
      private Boolean NoControlButtons; 

      public BaseForm() 
      { 
       InitializeComponent(); 
       TitleLabel.Visible = DialogStyle = true; 
       ControlBox = NoControlButtons = true; 
      } 

      public Boolean DialogForm 
      { 
       get 
       { 
        return DialogStyle; 
       } 
       set 
       { 
        DialogStyle = TitleLabel.Visible = value; 
        DialogStyle = CommandPanel.Visible = value; 
       } 
      } 

      public Boolean ControlButtons 
      { 
       get 
       { 
        return NoControlButtons; 
       } 
       set 
       { 
        NoControlButtons = ControlBox = value; 
       } 
      } 

      protected override void OnTextChanged(EventArgs e) 
      { 
       base.OnTextChanged(e); 
       TitleLabel.Text = Text; 
      } 
     } 
    } 
相關問題