MaskedTextBox.ValidatingType Property
如果要将自己的自定义数据类型与 ValidatingType 一起使用,则必须实现一个将字符串作为参数的静态Parse方法。必须使用以下一个或两个签名来实现此方法:
public static Object Parse(string)
public static Object Parse(string, IFormatProvider)
它没有很好地描述,并且此代码与C#.net相关。我应该为vb.net做什么?
答案 0 :(得分:0)
文档微软说:您可以使用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