Question How do you change initial Text displayed from inherited Button when created on Form

devilgoblin

Member
Joined
May 31, 2010
Messages
5
Location
Texas
Programming Experience
5-10
If you create a control by inheriting from Button and the control is named "CustomButton", when you add the control to the form, the button text is initially "CustomButton1". How can you make the initial text different than the name of the control?

For example, say I want the text to just display "Button".

I have tried setting the text on the New() constructor and overriding different subroutines, but have had no luck finding the correct subroutine or property to override.
 

devilgoblin

Member
Joined
May 31, 2010
Messages
5
Location
Texas
Programming Experience
5-10
You probably get in trouble if you don't ensure unique control names, but look here: http://www.vbdotnetforums.com/ide/45453-ccommon-controls-toolbox-new-objects-names.html
Not quite what I want. When you drag a normal TextBox to a form, for a split second you see "TextBox1", before it disappears and the Text property is then "".

I am guessing I still need to implement a Design feature as you did with that example, mainly because I am having the same basic trouble with another control. The Button issue is trivial, because chances are the Text will need to be changed anyway, but for this control inherited from TextBox it is not:

Code:
Imports System.ComponentModel

<Designer(GetType(EditBoxDesigner))>
Public Class EditBox
    Inherits TextBox

End Class

Public Class EditBoxDesigner
    Inherits System.Windows.Forms.Design.ControlDesigner

End Class
I drop this on the form and it will have "EditBox1", "EditBox2", etc as the Text property. I would like the text property to be blank like the regular textbox. Since I am using my own Control Designer, it uses the class name. If you take out the custom designer, the text is blank, I assume because it inherits the base designer.

Any ideas how I can JUST change the Text Property of an inherited control when dropped on a form?
 
Last edited:

devilgoblin

Member
Joined
May 31, 2010
Messages
5
Location
Texas
Programming Experience
5-10
This is how to fix the issue for any control where you want to change the text at design time:
Code:
Imports System.ComponentModel

<Designer(GetType(EditBoxDesigner))>
Public Class EditBox
    Inherits TextBox

End Class

Public Class EditBoxDesigner
    Inherits System.Windows.Forms.Design.ControlDesigner

    Public Overrides Sub InitializeNewComponent(ByVal defaultValues As IDictionary)

        MyBase.InitializeNewComponent(defaultValues)

        ' Clear the text field set by Control designer.
        Dim TextProperty As PropertyDescriptor = _
            TypeDescriptor.GetProperties(Component)("Text")
        If TextProperty IsNot Nothing _
            AndAlso TextProperty.PropertyType Is GetType(String) _
            AndAlso (Not TextProperty.IsReadOnly) _
            AndAlso TextProperty.IsBrowsable Then
            TextProperty.SetValue(Component, "")
        End If
    End Sub

End Class
Code:
TextProperty.SetValue(Component, "")
You can change it to whatever text you want the component to have when created at design time.
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,410
Location
Norway
Programming Experience
10+
I must have misread your first post. That can be done simpler like this in InitializeNewComponent:
Me.Control.Text = "EditBox"
 
Top Bottom