仅允许文本框中的数值?

时间:2015-04-04 01:50:34

标签: vb.net

我需要在文本框中控制用户的输入,那么我该怎么做? 谢谢

2 个答案:

答案 0 :(得分:0)

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
            MessageBox.Show("Please enter numbers only")
            e.Handled = True
        End If
    End Sub

已经问过同样的问题

How to allow user to enter only numbers in a textbox in vb.net?

Enter numeric value only into textbox in visual basic

答案 1 :(得分:0)

If Asc(e.KeyChar) <> 8 Then
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
            e.Handled = True
        End If
End If