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.
Edit the designer file. For example, say I have a Form named Form1. So, I would have Form1.cs, Form1.resx, and Form1.Designer.cs in visual studio. If opening Form1.Designer.cs you will see this:
This is a warning, but you can still edit this file.
Here's a sample edit, I am setting label3 text to label1's name:
Now in the form designer in Visual Studio I see that label3 has label1's name:
A similiar approach could be used for your user control.
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.Then at the top of MyControl, I simply added a new attribute:
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.
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).