通过设计器添加的VB.Net项目未反映在代码上

时间:2014-06-23 15:19:37

标签: vb.net user-controls collectioneditor

我有一个usercontrol,它公开了自定义集合的属性。

以下是usercontrol中使用的代码。

Imports System.ComponentModel.DesignerSerializationVisibility

Public Class textbox
    Inherits System.Windows.Forms.TextBox
    Private _validation As New validationList

    <System.ComponentModel.DesignerSerializationVisibility(Content)>
    Public Property Validation As validationList
        Get
            Return _validation
        End Get
        Set(ByVal value As validationList)
            _validation = value
        End Set
    End Property
End Class

以下是此属性使用的集合类。

Imports System.Collections.ObjectModel
<Serializable()> Public Class validationList
    Inherits Collection(Of validationItem)

    Public Shadows Sub Add(ByVal item As validationItem)
        '//Check for duplicates
        Dim dupe As Boolean = False
        For n As Int32 = 0 To Items.Count - 1
            If Items(n).Key = item.Key Then
                dupe = True
                Exit For
            End If
        Next
        If dupe = False Then
            Items.Add(item)
        End If
    End Sub
End Class

以下是集合类使用的项目列表

<Serializable()> Public Class validationItem
    Private _key As validationTypes
    Private _value As String

    Public Sub New()
        '//Empty constructor is needed for serialization
    End Sub

    Public Sub New(ByVal k As validationTypes, ByVal v As String)
        _key = k
        _value = v
    End Sub

    Public Enum validationTypes
        Madatory = 1
        [Integer] = 2
        Numeric = 3
        [Decimal] = 4
        MaxValue = 5
        MinValue = 6
        MinLength = 7
        Email = 8
    End Enum

    Public Property Value As String
        Get
            Return _value
        End Get
        Set(ByVal Value As String)
            _value = Value
        End Set
    End Property

    Public Property Key As validationTypes
        Get
            Return _key
        End Get
        Set(ByVal value As validationTypes)
            _key = value
        End Set
    End Property
End Class

这是设计师代码在实施Pluntonix建议的解决方案之后的样子。

Dim ValidationItem1 As Testing_Project.validationItem = New Testing_Project.validationItem(Testing_Project.validationItem.validationTypes.MaxValue, "4")
Dim ValidationItem2 As Testing_Project.validationItem = New Testing_Project.validationItem(Testing_Project.validationItem.validationTypes.MinLength, "5")

Me.Textbox1.Validations.Add(ValidationItem1)
Me.Textbox1.Validations.Add(ValidationItem2)

我从设计师和收藏中添加了许多项目。我试图在运行时检索它们,但所有键都设置为0,值设置为Nothing。我需要通过设计器添加的确切项目列表才能使用,如何使其工作,以便通过设计器添加的实际值也存在于运行时。

0 个答案:

没有答案