【问题标题】:User Control properties not saving in designer用户控件属性未保存在设计器中
【发布时间】:2016-10-18 11:01:40
【问题描述】:

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

我向控件添加了两个属性,一个用于将更改标签文本的“ButtonText”,另一个用于图片框中的“Image”。我通读了微软关于控件属性的文档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”属性的值。但是,当我向自定义属性添加值时,它们会立即恢复为空。

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

【问题讨论】:

    标签: .net vb.net winforms user-controls


    【解决方案1】:

    您没有将任何内容保存到您的变量中:

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

    【讨论】:

    • 哇...我怎么会忽略这样的事情?谢谢!真的帮了我大忙!
    【解决方案2】:

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

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

    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
    

    【讨论】:

      猜你喜欢
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      相关资源
      最近更新 更多