C# 创建具有自定义属性的基本表单

标签 c# winforms radix 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 中为 DialogForm 设置了内容,但在运行时它会恢复为 True。

该网站上有另一篇文章提到了这一点,但我不明白它的解释。

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

最佳答案

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

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

关于C# 创建具有自定义属性的基本表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12467295/

相关文章:

c# - 如何从托管字节 [] 缓冲区获取字节**

c# - 带有自定义项模板文本的 wpf 组合框

c# - 统一游戏管理器。脚本只工作一次

python - 对于二进制分离,哪个会更好 :using list or divmod?

c++ - 递归基础转换时间复杂度分析

file - 不小心删除了page.xml。我该如何取回它?

c# - 基于具有几个小数位的 double 生成唯一的哈希码

c# - 为可以调整大小和居中定位的字符串创建矩形

c# - "Publish language"选项到底是什么?

C# Winform - 如何通过向 ReportViewer 传递 3 个变量(即 Reportname(RDL 文件)、SQLstring 和 Connectionstring)来显示实时报告