C#使用自定义属性创建基本表单

时间:2012-09-17 21:23:16

标签: c# winforms base inheritance

我遇到一个小问题,其中定义的自定义属性值没有粘贴在继承的表单中。

我的基本表单中的代码是:

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 for DialogForm中设置了它,但在运行时它会恢复为True。

本网站上还有另一篇帖子提到了这一点,但我不明白它的解释。

我还想创建一个允许我隐藏ControlBox的属性,那么我该如何添加呢?

1 个答案:

答案 0 :(得分: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;
            }
        }
    }