Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I set the label of my UserControl at design time?

Tags:

c#

winforms

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.

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.

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.

like image 357
Jerry Avatar asked Nov 08 '11 15:11

Jerry


2 Answers

What about overriding the OnLoad method, which calls the Load event?

If that doesn't work, it's a bit sloppy, but you could use the TextChanged event, or the SizeChanged event as these are both called after the Name property is set by the designer (it sets the values in alphabetical order).

like image 196
Connell Avatar answered Nov 02 '22 04:11

Connell


So, here's the final answer:

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];
    }
}

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; }
    }

    ...
}

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.

like image 2
Jerry Avatar answered Nov 02 '22 03:11

Jerry