Only allow numeric values in the textbox

2019-02-16 23:05发布

I want to make a TextBox control that only accepts numerical values.

How can I do this in VB6?

10条回答
手持菜刀,她持情操
2楼-- · 2019-02-16 23:33

i usually use this code:

Private Sub text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(text1.Text & Chr(KeyAscii)) And Not KeyAscii = 8 Then KeyAscii = 0
End Sub
查看更多
Bombasti
3楼-- · 2019-02-16 23:37

The following may be used for whole numbers:

Private Sub text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(text1.Text & Chr(KeyAscii)) And Not KeyAscii = 8 Then    KeyAscii = 0
    if (KeyAscii>=43) and (KeyAscii<=46) Then KeyAscii = 0 
    'it ignores '-', '+', '.' and ','
End Sub
查看更多
The star\"
4楼-- · 2019-02-16 23:41

I used this code in my project:

Private Sub txtReceiptID_KeyPress(KeyAscii As Integer)
Dim Keychar As String
If KeyAscii > 31 Then
    Keychar = Chr(KeyAscii)
    If Not IsNumeric(Keychar) Then
        KeyAscii = 0
    End If
End If

End Sub

查看更多
混吃等死
5楼-- · 2019-02-16 23:41

Try this code:

Private Sub Text1_Change()
    textval = Text1.Text
    If IsNumeric(textval) Then
        numval = textval
    Else
        Text1.Text = CStr(numval)
    End If
End Sub
查看更多
登录 后发表回答