用户控件属性未保存在设计器中

User Control properties not saving in designer

我已经在 VB.NET 应用程序上工作了大约两年,它的功能与 Windows Explorer shell 和文件浏览器的替代品差不多。我刚开始开发一个用户控件,它的作用类似于按钮,但由一个图片框和一个标签组成。单击项目时发生的代码已经完成,但我遇到了控件属性的问题;

我向控件添加了两个属性,一个用于将更改标签文本的 "ButtonText",一个用于图片框中的 "Image"。我通读了 Microsoft 关于控件属性的文档 Creating a Windows Form User Control),他们帮助我向控件添加了属性。

Private bttnTxt As String
Private bttnImg As Image

<Category("Appearance"), Description("The text displayed at the bottom of the button control")>
Public Property ButtonText() As String
    Get
        Return bttnTxt
    End Get
    Set(ByVal Value As String)
        Label3.Text = Value
    End Set
End Property

<Category("Appearance"), Description("The image used in the button control")>
Public Property Image() As Image
    Get
        Return bttnImg
    End Get
    Set(ByVal Value As Image)
        PictureBox3.BackgroundImage = Value
    End Set
End Property

我通过解决方案构建,将新添加的控件添加到我的应用程序主窗体的设计器中,并设置 "Image" 和 "ButtonText" 属性的值。但是,当我向我的自定义属性添加一个值时,它们会立即恢复为空。

我需要帮助来确定为什么我在设计器中设置的值不会保留在属性中。

您没有向变量保存任何内容:

Public Property ButtonText() As String
  Get
    Return bttnTxt
  End Get
  Set(ByVal Value As String)
      bttnTxt = Value
      Label3.Text = Value
  End Set
End Property

我的问题是我需要重写克隆函数。请参阅下面的代码示例。

希望这有助于为某人节省一些时间。

Public Class CustomClass_DatGridViewColumn 
    Inherits DataGridViewComboBoxColumn

    Private propertyValue As String = ""

    Public Overrides Function Clone() As Object
        Dim col As CustomClass_DatGridViewColumn = CType(MyBase.Clone(), CustomClass_DatGridViewColumn)
        col.myProperty = propertyValue
        Return col
    End Function

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Category("Data"), .Description("description")>
    Public Property myProperty As String
        Get
            Return propertyValue 
        End Get
        Set(ByVal value As String)
            propertyValue = value
        End Set
    End Property
End Class