C# 创建自定义控件
我正在尝试根据我使用的特定配置创建一些自定义控件。
我的第一个就是创建一个标准退出按钮。然而,它并不像我想的那样工作。本质上,我希望将默认文本添加到表单时为“退出应用程序”。不幸的是,我想到的两种方法实际上并没有显示这一点,而是显示了按钮类的名称,即“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";
}
}
I am having a go at creating some custom controls based on specific configurations that I use.
My first one, is simply to create a standard exit button. However, it's not working how I think. Essentially, I want the default text to be "Exit Application", when it is added to the form. Unfortunately, the two ways that I have thought of, dont actually show that, instead it shows the name of the button class, i.e. "CustomExitButton1". This is the two ways I have tried:
public class StandardExitButton : Button
{
public StandardExitButton()
{
this.ControlAdded += new ControlEventHandler(StandardExitButton_ControlAdded);
}
void StandardExitButton_ControlAdded(object sender, ControlEventArgs e)
{
Text = "Exit Application";
}
}
And:
public class StandardExitButton : Button
{
public StandardExitButton()
{
this.Text = "Exit Application";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我使用了一堆按钮来完成此操作,例如“添加”、“编辑”、“删除”、“取消”、“确定”、“退出”等...此外,还使用我自己的标准字体大小、颜色保留所有按钮、标签、文本框等等等...对于文本,在每个子类按钮上,我做了类似的事情...
永远不必担心“setter”,因为按钮的文本永远不应该在设计器或派生中更改...
编辑 - 更完整示例...
然后,当您将 MyExitButton 或 MyAddButton 的实例放在表单上时,它将强制为您添加标题,并阻止您更改它,因为它是相应类的只读属性。
I've done this with a bunch of buttons, such as Add, Edit, Delete, Cancel, Ok, Exit, etc... In addition to keeping all my buttons, labels, textboxes, etc with my own standard font sizes, colors, etc ... For the text, on each subclassed button, I did something like ...
Never have to worry about a "setter" since the button's text should never change in the designer or derived...
EDIT -- more complete sample...
Then, when you put an instance of the MyExitButton or MyAddButton on the form, it will FORCE the caption for you and also prevent you from changing it as it is a read-only property of the respective class.
ControlAdded 事件处理程序无法按您想象的方式工作,请参阅 这个示例
可以吗?
the ControlAdded event handler doesn't work the way you think, please refer to this example
does this work?
当一个控件添加到您的控件时,ControlAdded 将触发 - 不会当您的控件添加到另一个控件时。 (您可以尝试使用 ParentChanged 事件。)
ControlAdded will fire when a control is added to your control - not when your control is added to another control. (You could try the ParentChanged event instead.)
您必须做两件事:
将 Text 属性更改为私有(以防止设置它):
设置默认文本:
You have to do 2 things:
Change Text property to private (to prevent setting it):
Set default text:
您可能需要覆盖按钮
public override string Text { get;放; }
属性You probably need to override Buttons
public override string Text { get; set; }
Property啊 - 这里的问题是设计器系统执行以下操作:
(执行中的代码
构造函数),所以你的文本是“退出
应用程序”的时间很短。
然后将财产交给设计师
显式地将文本设置为名称
的控制。
当我构建自定义表单设计器时,我已经解决了这个问题(这应该超出您正在做的范围)。我无法想出一种方法来克服这种行为......
Ahh - the problem here is the designer system does the following:
(executing the code in the
constructor), so your text is "Exit
Application" for a very short time.
property to, the designer then
explicitly sets the text to the name
of the control.
I've solved this when I was building a custom forms designer (which should be out of the scope of what you're doing). I can't think of a way off the top of my head to override that behavior...
怎么样
How about something like
您无法使用 Visual Studio 设计器看到它,而是需要使用代码加载它并运行应用程序才能查看您指定的文本。
You can't see it by using the visual studio designer, instead you need to load it with the code and run your application to see the Text as you specified.