如何在VB.NET 中限制文本框中只能输入数字
发布网友
发布时间:2022-05-09 20:23
我来回答
共2个回答
热心网友
时间:2023-10-17 16:44
以下是只能输入数字和小数点,并且小数点只能输入一次
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
If Char.IsDigit(e.KeyChar) or e.KeyChar = Chr(8) or e.KeyChar = "." Then
If e.KeyChar = "." And InStr(TextBox1.Text, ".") > 0 Then
e.Handled = True
Else
e.Handled = False
End If
Else
e.Handled = True
End If
End Sub
热心网友
时间:2023-10-17 16:45
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case Asc(e.KeyChar)
Case 8, 45, 46 '退格,负号,小数点
Case 48 To 57 '0~9
Case Else
e.KeyChar = ""
End Select
End Sub