2012-09-17 57 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; 
      } 
     } 
    } 
相关问题