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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With