【问题标题】:C# Creating a Base Form with Custom PropertiesC# 创建具有自定义属性的基本表单
【发布时间】:2012-09-10 03:26:52
【问题描述】:

我遇到了一个小问题,即定义的自定义属性值没有保留在继承的形式中。

我的基本形式的代码是:

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,那么如何添加呢?

【问题讨论】:

  • @DJKRAZE - 那是我所指的另一篇文章,但我不明白仅执行上述操作有什么问题。与另一篇文章中的答案相比,我需要对我的代码做些什么不同的事情,因为我有两件事要设置。
  • 对于初学者,您尝试存储和/或访问哪些属性和/或值..在初始化之后的链接中的含义注意链接如何初始化然后_一些控件名称..例如跨度>

标签: c# winforms base inheritance


【解决方案1】:

我相信我现在已经做到了:

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;
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多