问题
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