Nelson Hoover
Nelson Hoover

Reputation: 169

Text property in custom Control loses value

I am making a custom button control and am having some difficulty with my Text property. Anything I type in only stays while the form designer window is open. When I close the form designer and reopen it, my Text property resets to "". Also if I run the program, it loses the value entered at design time.

I also have an Image property for my control which is working just fine.

Here's some of my code:

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class BlackButton

Private iText As String
Private iImage As Image

''' <summary>
''' Gets/Sets the text displayed in the button.
''' </summary>
<Browsable(True), Description("Gets or sets the text displayed on the button")> _
Public Shadows Property Text() As String
    Get
        Return iText
    End Get
    Set(ByVal value As String)
        iText = value
        ReDrawMe()
    End Set
End Property

''' <summary>
''' Gets/Sets the image to be displayed on the button
''' </summary>
<Browsable(True), Description("Gets or sets the image displayed on the button")> _
Public Shadows Property Image() As Image
    Get
        Return iImage
    End Get
    Set(ByVal value As Image)
        iImage = value
        ReDrawMe()
    End Set
End Property

I have carefully combed through my code and made sure I'm not resetting it anywhere.

Thanks in advance for any help on this.

Upvotes: 3

Views: 2998

Answers (2)

olivier_sdg
olivier_sdg

Reputation: 1538

It seems to work adding a property :

<Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Overrides Property Text() As String
    Get
        Return MyBase.Text
    End Get
    Set(ByVal value As String)
        MyBase.Text = value
        LabInfo.Text = MyBase.Text
    End Set
End Property

Upvotes: 1

Luke Vo
Luke Vo

Reputation: 20668

I once meet this problem. Just delete the Shadows keyword. I don't know if Override can work there, but if not, just ignore the VS warning about Text and Image properties.

EDIT: I don't know why you didn't succeed with the Overrides keyword. Only Image property forced me using Overloads instead. Here's my code:

Imports System.ComponentModel

Public Class UserControl1

Dim _Text As String
Dim _Image As Image

<Browsable(True), Description("Gets or sets the text displayed on the button")> _
Overrides Property Text() As String
    Get
        Return _Text
    End Get
    Set(ByVal value As String)
        _Text = value
        'This line just for update
        'the UI when I design to check
        'if the values are saved.
        MyBase.Text = value
    End Set
End Property

<Browsable(True), Description("Gets or sets the image displayed on the button")> _
Overloads Property Image() As Image
    Get
        Return _Image
    End Get
    Set(ByVal value As Image)
        _Image = value
        'ReDrawMe()
    End Set
End Property

End Class

Upvotes: 1

Related Questions