如何在设计时设置 UserControl 的标签? [英] How would I set the label of my UserControl at design time?

查看:24
本文介绍了如何在设计时设置 UserControl 的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置如下:我创建了一个用户控件 MyUserControl.在这个控件上有一个简单的标签.当我在窗体上放置此控件的新实例时,我希望标签是控件的名称.例如,如果这是控件的第一个实例,VStudio 会自动将其名称设置为myUserControl1".我只想让标签读取myUserControl1".如果这是第五个控件,VStudio 会将其命名为 myUserControl5.我希望标签相应地更改...但前提是尚未设置.一旦我将它设置为标签",它就应该被留下.

Here's the setup: I've created a user control MyUserControl. On this control is a simple label. When I drop a new instance of this control on a form, I would like the label to be the name of the control. For example, if this is the first instance of the control, VStudio automatically sets the name of it to be "myUserControl1". I would simply like the label to read "myUserControl1". If this is the fifth control, VStudio will name it myUserControl5. I would like the label to change accordingly... but only if it hasn't already been set. Once I set it to read "The Label", it should be left alone.

我认为这很容易.在构造函数中,设置 label1.Text = this.Name.但后来我意识到,在实例化时 this.Name 只是 MyUserControl.它尚未被 VisualStudio 和 CONTAINING InitializeComponent() 方法命名.

I thought it would be easy. In the constructor, set label1.Text = this.Name. But then I realized, at instantiation this.Name is simply MyUserControl. It hasn't been named yet by VisualStudio and the CONTAINING InitializeComponent() method.

啊!我将覆盖名称"字段.设置后,检查 DesignMode 并更新标签!...但我无法覆盖名称.它不是虚拟的或抽象的.我可以使用new"来强制它,但是,它永远不会触发,从而破坏了目的.

Ah! I'll just override the "Name" field. When it gets set, check for DesignMode and update the label!... but I can't override the name. It's not virtual or abstract. I can use "new" to force it, but then, it never fires, thus defeating the purpose.

我可以使用哪个事件来表示设计者何时为控件命名?我知道雷德蒙德的人总是在表单上删除控件时这样做,所以这是很有可能的.我真的被这个小谜语难住了.

Which event can I use to denote when the designer has named the control? I know that the redmond folks do it all the time when they drop a control on the form, so it's very possible. I'm actually quite stumped by this little riddle.

推荐答案

所以,这是最终答案:

根据 Hans 的建议,它确实需要 ControlDesigner.我创建了一个简单的设计器,它可以与一些内部属性交互,并且我确实让它工作了.不过,这里有一个问题:这有点像黑客.似乎没有一个字段具有我要查找的值,因此我必须对对象执行 .ToString(),然后解析出第一部分.我认为这将是最干净的.

Per Hans' suggestion, it did require a ControlDesigner. I created a simple designer that would interface with some internal properties, and I did get it to work. Here's the catch, though: It's a bit of a hack. There doesn't seem to be a single field that has the value I'm looking for, so I had to do a .ToString() on an object, then parse out the first section. I figured that's about as clean as it is going to get.

public class MyControlDesigner : ControlDesigner
{
    public MyControlDesigner()
    {
    }


    public override void InitializeNewComponent(IDictionary defaultValues)
    {
        base.InitializeNewComponent(defaultValues);

        MyControl control1 = this.Component as MyControl;
        control1.LabelText = control1.ToString().Split(" ".ToCharArray())[0];
    }
}

然后在 MyControl 的顶部,我简单地添加了一个新属性:

Then at the top of MyControl, I simply added a new attribute:

[DesignerAttribute(typeof(MyControlDesigner))]
public partial class MyControl : UserControl
{
    ...

    // Using the property "Text" Causes Visual Studio to crash!!!
    public string LabelText
    {
        get { return label1.Text; }
        set { label1.Text = value; }
    }

    ...
}

如您所见,它不是很复杂,但有点像黑客.当我尝试覆盖文本"时,控件锁定了 IDE.不知道为什么,但当我称它为LabelText"时,它工作得很好.

As you can see, it wasn't very complicated, but it is a bit hack-ish. The control locked up the IDE when I tried to override "Text". Not sure why, but when I called it "LabelText", it worked perfectly.

这篇关于如何在设计时设置 UserControl 的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆