【问题标题】:how to create your own custom data types with Validating Type如何使用验证类型创建自己的自定义数据类型
【发布时间】:2019-07-19 16:30:22
【问题描述】:

MaskedTextBox.ValidatingType Property

如果您想通过 ValidatingType 使用您自己的自定义数据类型,您必须实现一个将字符串作为参数的静态 Parse 方法。此方法必须使用以下一个或两个签名来实现:

public static Object Parse(string)

public static Object Parse(string, IFormatProvider)

没有很好的描述,这段代码与 C#.net 相关。我应该为 vb.net 做什么?

【问题讨论】:

    标签: vb.net static maskedtextbox


    【解决方案1】:

    文档微软说:可以使用ValidatingType来验证用户输入的数据是否在正确的范围内。

    在这个例子中,我们想要一个没有空格和字母的 9 位代码:

    012345678 --> 有效输入(但零会被删除。我们可以取字符串的类型或十位数字,但不计算十位数字。)

    999999999 --> 有效输入

    88_88_888 --> 无效输入

    8888_____ --> 无效输入

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As 
    System.EventArgs)Handles MyBase.Load
        Me.MaskedTextBox1.Mask = "000-000-000" 'My custom mask 
        Me.MaskedTextBox1.ValidatingType = GetType(myCustomType)   
        Me.ToolTip1.IsBalloon = True
    End Sub
    
    Structure myCustomType
        Public code As Integer
        Public Shared Function Parse(s As String) As myCustomType
            s = s.Trim
            If s.Length = 9 Then
                Dim newObject As New myCustomType
                newObject.code = UInt32.Parse(s)
                Return newObject 'Valid
            Else
                Return Nothing 'Invalid
            End If
        End Function
    End Structure
    
    Private Sub MaskedTextBox1_TypeValidationCompleted(ByVal sender As Object, ByVal e As 
    TypeValidationEventArgs) Handles MaskedTextBox1.TypeValidationCompleted
        If (Not e.IsValidInput) Then
            Me.ToolTip1.ToolTipTitle = "Invalid Date"
            Me.ToolTip1.Show("your message", Me.MaskedTextBox1, 0, -20, 5000)
        Else
            ' Now that the type has passed basic type validation, enforce more specific 
            'type rules.        
            'Anything you want to do after the user input is correct. write here...
        End If
    End Sub
    
    ' Hide the tooltip if the user starts typing again before the five-second 
    ' display limit on the tooltip expires.
    Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) 
    Handles MaskedTextBox1.KeyDown
        Me.ToolTip1.Hide(Me.MaskedTextBox1)
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 2016-05-07
      • 1970-01-01
      • 2021-01-27
      • 2017-02-20
      • 1970-01-01
      相关资源
      最近更新 更多