Default name stored in Text property

IfYouSaySo

Well-known member
Joined
Jan 4, 2006
Messages
102
Location
Hermosa Beach, CA
Programming Experience
3-5
So when a TextBox gets created, the Name property is set to "TextBox1", and so is the Text property. I'm wondering how to get the same effect when I have my own UserControl, i.e. if my class is MyCustomControl, then when the user drags it onto a form I get the Text field set to MyCustomControl1, MyCustomControl2, etc.
 
I don't see that the Text is set in VS2005, it did in VS2003 (and I think it was annoying). It is the IDE that is naming the controls, it does this based on the class name of the control and adds a counter to ensure no control gets the same variable name, that have to refer to the control instance in forms generated code.

The place to do Designer related work with the control if not in the class template would be through an ControlDesigner for the class, but if you try to do this for example in the Initialize override you will still only have the type name (it also don't change generated code), the IDE has not yet changed the name of the control at this point, actually while the property displays this value is does not take effect until runtime when the control is instantiated. In code view you also use this name, but here it only refer to the variable that in runtime will point to the instance, and again Intellisense will play along and is a feature of the IDE. So I don't know if it is possible for the control to find this name at design time. You also see in the properties pane it is written "(Name)", the paranthesis probably mean it is 'special'. If you use the OnMouseEnter+Leave overrides to set the Text you'll also see the control Name remains same as control class name anytime at design time.
 
Have been looking more into this and found more help with IComponentChangeService.

Initially it still get the class name, but when Name is changed (by developer) it updates the Textbox also. If you comment out the Name directive in designer generated code (in theclass.Designer.vb file) it will also initially get the correct name assigned by IDE, but any change to the development of the UserControl in designer will generate this again and set the Name again.

Here is an example UserControl with a controldesigner assigned, added a property to retain the Text value of child TextBox at runtime:
VB.NET:
Expand Collapse Copy
Imports System.Windows.Forms.Design
Imports System.ComponentModel

<Designer(GetType(mydesigner))> _
Public Class UserControl2

    Public Property tbtext() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal value As String)
            TextBox1.Text = value
        End Set
    End Property

End Class
Here is the inherited ControlDesigner:
VB.NET:
Expand Collapse Copy
Class mydesigner
    Inherits ControlDesigner

    Private WithEvents iccs As Design.IComponentChangeService
    Private uc As UserControl2

    Public Overrides Sub Initialize(ByVal component As IComponent)
        MyBase.Initialize(component)
        iccs = GetService(GetType(Design.IComponentChangeService))
        uc = component
        uc.tbtext = uc.Name
    End Sub

    Private Sub iccs_ComponentRename(ByVal sender As Object, ByVal e As Design.ComponentRenameEventArgs) Handles iccs.ComponentRename
        uc.tbtext = e.NewName
    End Sub
End Class
Also worth mentioning is I got Invalid Property Value errors frequently when testing this out and had to close project and reopen it, look to me as a VS bug. (I have SP1)
 
Back
Top