C# Creating a Base Form with Custom Properties

坚强是说给别人听的谎言 提交于 2019-12-12 19:49:28

问题


I am having a small issue where the defined custom property value is not sticking in the inherited form.

The code in my base form is:

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

Then in the form that inherits this I have:

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

For some reason, despite what I set in MainForm for DialogForm, on runtime it reverts back to True.

There is another post on this site which mentions this, but I don't get what it explains.

I also want to create a property which allows me to hide the ControlBox, so how do I add this in?


回答1:


I believe I have done it now:

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


来源:https://stackoverflow.com/questions/12467295/c-sharp-creating-a-base-form-with-custom-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!