这是我的代码:
Private Sub prices_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles wholeprice_input_new_item.KeyPress, dozenprice_input_new_item.KeyPress, detailprice_input_new_item.KeyPress, costprice_input_new_item.KeyPress
Dim TxtB As TextBox = CType(sender, TextBox)
Dim rex As Regex = New Regex("^[0-9]*[.]{0,1}[0-9]{0,1}$")
'MsgBox(TxtB.Text())
If (Char.IsDigit(e.KeyChar) Or e.KeyChar.ToString() = "." Or e.KeyChar = CChar(ChrW(Keys.Back))) Then
If (TxtB.Text.Trim() <> "") Then
If (rex.IsMatch(TxtB.Text) = False And e.KeyChar <> CChar(ChrW(Keys.Back))) Then
e.Handled = True
End If
End If
Else
e.Handled = True
End If
End Sub
文本框的 Text 属性不包括最后按下的字符,例如:
Text entered = "12.1"
TxtB.Text = "12."
Text entered = "11.."
TxtB.Text = "11."
Text entered = "12"
TxtB.Text = "1"
我想验证所有字符。如何使事件按键验证文本框中的所有字符?