C# 创建自定义控件

发布于 2024-10-18 17:00:28 字数 669 浏览 2 评论 0原文

我正在尝试根据我使用的特定配置创建一些自定义控件。

我的第一个就是创建一个标准退出按钮。然而,它并不像我想的那样工作。本质上,我希望将默认文本添加到表单时为“退出应用程序”。不幸的是,我想到的两种方法实际上并没有显示这一点,而是显示了按钮类的名称,即“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

寂寞陪衬 2024-10-25 17:00:28

我使用了一堆按钮来完成此操作,例如“添加”、“编辑”、“删除”、“取消”、“确定”、“退出”等...此外,还使用我自己的标准字体大小、颜色保留所有按钮、标签、文本框等等等...对于文本,在每个子类按钮上,我做了类似的事情...

[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 的实例放在表单上时,它将强制为您添加标题,并阻止您更改它,因为它是相应类的只读属性。

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 ...

[ReadOnly(true)]
public override string Text
{ get { return "&Exit Application"; } }

Never have to worry about a "setter" since the button's text should never change in the designer or derived...

EDIT -- more complete sample...

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()
    {}
}

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.

脸赞 2024-10-25 17:00:28
public StandardExitButton()
{
    this.Text = "Exit Application";
}

ControlAdded 事件处理程序无法按您想象的方式工作,请参阅 这个示例

可以吗?

public StandardExitButton()
{
    this.Text = "Exit Application";
}

the ControlAdded event handler doesn't work the way you think, please refer to this example

does this work?

三岁铭 2024-10-25 17:00:28

当一个控件添加到您的控件时,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.)

爱格式化 2024-10-25 17:00:28

您必须做两件事:

将 Text 属性更改为私有(以防止设置它):

private new string Text  
{  
    get  
    {  
        return base.Text;  
    }  
    set  
    {  
        base.Text = value;  
    }  
}  

设置默认文本:

public StandardExitButton()  
{  
    this.Text = "Exit Application";  
}  

You have to do 2 things:

Change Text property to private (to prevent setting it):

private new string Text  
{  
    get  
    {  
        return base.Text;  
    }  
    set  
    {  
        base.Text = value;  
    }  
}  

Set default text:

public StandardExitButton()  
{  
    this.Text = "Exit Application";  
}  
够钟 2024-10-25 17:00:28

您可能需要覆盖按钮 public override string Text { get;放; } 属性

You probably need to override Buttons public override string Text { get; set; } Property

滥情哥ㄟ 2024-10-25 17:00:28

啊 - 这里的问题是设计器系统执行以下操作:

  • 创建控件的实例
    (执行中的代码
    构造函数),所以你的文本是“退出
    应用程序”的时间很短。
  • 接下来无论您设置什么文本
    然后将财产交给设计师
    显式地将文本设置为名称
    的控制。

当我构建自定义表单设计器时,我已经解决了这个问题(这应该超出您正在做的范围)。我无法想出一种方法来克服这种行为......

Ahh - the problem here is the designer system does the following:

  • Creates an instance of your control
    (executing the code in the
    constructor), so your text is "Exit
    Application" for a very short time.
  • Next no matter what you set the text
    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...

旧人 2024-10-25 17:00:28

怎么样

[DefaultValue("Exit Application")]
public override string Text
{
    get
    {
        return base.Text;
    }
    set
    {
        base.Text = value;
    }
}

How about something like

[DefaultValue("Exit Application")]
public override string Text
{
    get
    {
        return base.Text;
    }
    set
    {
        base.Text = value;
    }
}
鸠书 2024-10-25 17:00:28

您无法使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文