我正在开发 vb.net - windows 应用程序。
我有一个文本框,用户应该在其中输入数据。我已经进行了一些验证,例如,用户只能输入数字、字母和逗号。(没有其他符号。)它工作正常,但我想限制只有一个逗号应该插入不超过一个。
这个怎么做 ?
我有以下文本框按键事件的代码。
ReadOnly ValidChars As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,"
Private Sub txtNewFlatName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtNewFlatName.KeyPress
Select Case e.KeyChar
Case Convert.ToChar(Keys.Enter) ' Enter is pressed
' Call method here...
Case Convert.ToChar(Keys.Back) ' Backspace is pressed
e.Handled = False ' Delete the character
Case Chr(22) ' CTRL+V is pressed
' Paste clipboard content only if contains valid characters
e.Handled = If(Clipboard.GetText().All(Function(c) ValidChars.Contains(c)), False, True)
Case Else ' Other key is pressed
e.Handled = Not (ValidChars.IndexOf(e.KeyChar) > -1)
End Select
End Sub