I have a custom version of a label control (built using a user control). While working in the designer, I want to intercept the setting of the Name property (in the properties panel) and use it to generate the Text property. That is, if I enter “lblFirstName” into the Name property of the properties panel I want to immediately see that the Text property is set to “First Name”.
Parsing the Name property is not the issue; I can do that.
I have tried to overload/shadow the Name property (since “Overrides” is not allowed) to essentially add this “aspect” to our custom label control but it doesn’t seem to hit the Shadowed method at design time. It does hit the Shadowed method at run time if manipulated via code.
The point is to avoid double the work as the label text and the label name are essentially the same. The only difference is one is formatted to be human friendly and the other machine friendly.
<System.ComponentModel.Browsable(True),
System.ComponentModel.ParenthesizePropertyName(),
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible)>
Public Shadows Property Name As String
Get
Return MyBase.Name
End Get
Set(value As String)
MyBase.Name = value
If Me.DesignMode Then
Me.Text = Humanize(value)
End If
Me.Invalidate()
End Set
End Property
This may be a matter of picking the right attributes. I’m not sure.
Conversely, if it’s an easier alternative, we could allow setting the Text property to set the Name property. I doubt this would be easier since it should also reflect the new name in the Designer.vb code, not just in the label’s Name property itself.
You can do this with some extra design time elements. The
IComponentChangeServiceis your hook to design time change events.MSDN – IComponentChangeService
MSDN – ISite
EDIT : added disposing code!