如何在VB.NET 中限制文本框中只能输入数字

如题所述

以下是只能输入数字和小数点,并且小数点只能输入一次
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
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-11-24
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本回答被网友采纳
相似回答