VB.NET中让Textbox只能输入数字
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp If (e.Keyvalue > 47 And e.Keyvalue < 58) Or (e.Keyvalue > 95 And e.Keyvalue < 106) Or (e.Keyvalue = 8) Or (e.Keyvalue = 45) Or (e.Keyvalue = 46) Then str = TextBox1.Text Else TextBox1.Text = str TextBox1.Focus() End If End Sub限制文本框的输入(只输入数字)
文本框防止非法字符输入:
只输入整数: Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case Asc("0") To Asc("9"), vbKeyBack 'nop Case Else KeyAscii = 0 End Select End Sub
只输入小数:
Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case Asc("0") To Asc("9"), vbKeyBack 'nop
case Asc(".")'允许一个小数点 If InStr(1, Text1.Text, ".") > 0 Then KeyAscii = 0
Case Else KeyAscii = 0
End Select End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Dim num As Integer = 0 Dim temp As String = "" While num < TextBox1.Text.Length temp += "[0-9]" num += 1 End While
If Not (TextBox1.Text Like temp) Then TextBox1.Text = str TextBox1.Focus() Else str = TextBox1.Text End If End Sub End Class
- 联系我们: QQ:82526114(技术) 411523648(客服) 237057746(财务)
- 电话:+86-762-4372098 邮箱:webmaster@814e.net,support@814e.net
- 粤ICP备05002242号 网监局备案:4403701910502
