为不同的类型创建自定义TypeConverter

时间:2016-03-05 10:15:31

标签: .net vb.net winforms

嗨,我一直在思考如何实现以下目标:

我需要创建一个组件' ControlValidator'扩展名为Intent i = new Intent(arg0, aani.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); arg0.startActivity(i); 的属性,其类型为Validator,该属性应作为ComboBox显示在属性网格中,其值为ValidatorBase,其中:

  • 无=无验证
  • 必填字段=从{None, Required Field, Regular Expression}
  • 延伸的类
  • 正则表达式=从ValidatorBase
  • 延伸的类

这是包含我的代码的属性网格,它显示了我想要的属性,但是我无法将Validation1设置为“无”#39;如果我尝试保存表单,它会说我分配属性的Button生成一个空值:

enter image description here

这是我的代码,我很困惑哈哈。

谢谢!

ControlValidator

ValidatorBase

ValidatorsBase

<ProvideProperty("Validation1", GetType(Control))>
Public Class ControlValidator
    Inherits Component

    Implements IExtenderProvider

    Private helpTexts As Hashtable

    Public Sub New()
        helpTexts = New Hashtable
    End Sub

    Public Function CanExtend(extendee As Object) As Boolean Implements IExtenderProvider.CanExtend
        If TypeOf extendee Is Control AndAlso Not TypeOf extendee Is ControlValidator Then
            Return True
        Else
            Return False
        End If
    End Function

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
    <DefaultValue(GetType(ValidatorsBase), "None")>
    Public Function GetValidation1(ByVal ctrl As Control) As ValidatorsBase
        Dim myText As ValidatorsBase = helpTexts(ctrl)

        If myText Is Nothing Then
            Return Nothing
        End If

        Return myText
    End Function

    Public Sub SetValidation1(ByVal ctrl As Control, ByVal value As ValidatorsBase)
        If value Is Nothing Then
            helpTexts.Remove(ctrl)
        Else
            helpTexts(ctrl) = value
        End If
    End Sub

End Class

必填字段类:

<TypeConverter(GetType(ValidatorsConverter))>
Public Class ValidatorsBase
    Private _errorText As String
    Public Property ErrorText() As String
        Get
            Return _errorText
        End Get
        Set(ByVal value As String)
            _errorText = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return "Validator"
    End Function
End Class

的TypeConverter:

<TypeConverter(GetType(ValidatorsConverter))>
Public Class RequiredField
    Inherits ValidatorsBase

    Private _required As Boolean
    Public Property required() As Boolean
        Get
            Return _required
        End Get
        Set(ByVal value As Boolean)
            _required = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return "Required Field"
    End Function
End Class

1 个答案:

答案 0 :(得分:1)

你的方法存在一些缺陷。 ValidatorsBase显然也用作ErrorText版本,RegularExpression类型未显示。

  • 您要扩展的属性属于ValidatorsBase类型,但这不允许您为更具体的类型设置RequiredRegEx文本属性。这些属性存在于其他类型。
  • 如果None的属性保留为null,则VS IDE不会喜欢它;也不会因为你需要做很多If thisCrtl.Validator1 IsNot Nothing Then...
  • 我不知道如何使用这些对象,但你根本就没有创建任何对象,因此任何设计师的作业都会丢失。

如果你抽象一点,看起来可能有3条信息:验证规则(无,ErrorMsg,必需,RegEx),某些文字用作msg或RegEx取决于规则,布尔表示必需。

所以,使用一个简单的类,基本的ExpandableObjectConverter确实接近你想要的:

' simple, easy TypeConverter to expand the object to reveal the properties
<TypeConverter(GetType(ExpandableObjectConverter))>
Public Class Validator
    Public Property Rule As ControlValidator.ValidationRule
    Public Property ValidationText() As String
    <Description("This field is Required By Law")>
    Public Property Required As Boolean

    Public Sub New()

    End Sub

    Public Sub New(r As ControlValidator.ValidationRule)
        Rule = r
    End Sub

    Public Overrides Function ToString() As String
        Return Rule.ToString()
    End Function
End Class

然后,Extender:

<ProvideProperty("Validator", GetType(Control))>
Public Class ControlValidator
    Inherits Component
    Implements IExtenderProvider

    Public Enum ValidationRule
        None
        RequiredField
        RegularExpression
    End Enum

    Private helpTexts As Dictionary(Of Control, Validator)

(提示:如果您最初将CanExtend限制为一个控件,例如TextBoxNumericUpDn,那么当您改变对名称的看法时,您将无法使数百行设计器代码失效或财产类型。)

您的获取/设置对需要创建它们:

<Category("Uber Validator"), DisplayName("Validator")>
<Description("Validation type to apply to this control")>
Public Function GetValidator(ByVal ctrl As Control) As Validator
    If helpTexts.ContainsKey(ctrl) Then
        Return helpTexts(ctrl)
    Else
        Return New Validator(ValidationRule.None)
    End If
End Function

Public Sub SetValidator(ByVal ctrl As Control, ByVal value As Validator)
    If helpTexts.ContainsKey(ctrl) Then
        helpTexts(ctrl) = value
    Else
        helpTexts.Add(ctrl, value)
    End If
End Sub

如图所示进行装饰,您可以更改扩展属性的可怕默认显示:

enter image description here

到目前为止,VS能够为我们序列化Validator。设计师代码:

Validator2.Required = False
Validator2.Rule = ControlValidator.ValidationRule.None
Validator2.ValidationText = Nothing
Me.ControlValidator1.SetValidator(Me.nud2, Validator2)

原样,我的Validator只是您看似需要的信息的持有者。这提供了多种使用方法:

  • 该类可以根据规则
  • 有条件地实现验证逻辑
  • 课程可以创造&#34;真实&#34;内部辅助验证员
  • 您的代码可以获取数据并创建&#34;真实&#34;来自它的验证器对象

您可能会发现一个多功能验证器更有用:例如,ErrorText可能对RegExRequired规则都有用。