我正在尝试根据我使用的特定配置创建一些自定义控件。
我的第一个,只是创建一个标准的退出按钮。但是,它并不像我想的那样工作。本质上,我希望将默认文本添加到表单时为“退出应用程序”。不幸的是,我想到的两种方式实际上并没有显示出来,而是显示了按钮类的名称,即“CustomExitButton1”。这是我尝试过的两种方式:
public class StandardExitButton : Button
{
public StandardExitButton()
{
this.ControlAdded += new ControlEventHandler(StandardExitButton_ControlAdded);
}
void StandardExitButton_ControlAdded(object sender, ControlEventArgs e)
{
Text = "Exit Application";
}
}
和:
public class StandardExitButton : Button
{
public StandardExitButton()
{
this.Text = "Exit Application";
}
}
请您参考如下方法:
我用一堆按钮完成了这个,比如添加、编辑、删除、取消、确定、退出等...除了用我自己的标准字体保留我所有的按钮、标签、文本框等大小、颜色等...对于文本,在每个子类按钮上,我做了类似...
[ReadOnly(true)]
public override string Text
{ get { return "&Exit Application"; } }
永远不必担心“ setter ”,因为按钮的文本永远不会在设计器或派生中更改...
编辑——更完整的示例...
public class MyButton : Button
{
[ReadOnly(true)]
public sealed override Font Font
{ get { return new Font("Arial", 9F, FontStyle.Italic, GraphicsUnit.Point); } }
[ReadOnly(true)]
public sealed override Color ForeColor
{ get { return Color.Blue; } }
public GDITButton()
{ }
}
public class MyExitButton : MyButton
{
[ReadOnly(true)]
public override string Text
{ get { return "Exit"; } }
public MyExitButton()
{}
}
public class MyAddButton : MyButton
{
[ReadOnly(true)]
public override string Text
{ get { return "Add"; } }
public MyAddButton()
{}
}
然后,当您在表单上放置 MyExitButton 或 MyAddButton 的实例时,它会为您强制显示标题并阻止您更改它,因为它是相应类的只读属性。
