Only allow numeric values in the textbox

前端 未结 12 926
南笙
南笙 2021-01-12 03:53

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

How can I do this in VB6?

相关标签:
12条回答
  • 2021-01-12 04:08

    I usually use this code:

    Private Sub text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) And Not KeyAscii = 8 Then
            KeyAscii = 0
        End If
    End Sub
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-12 04:10

    In the text box text Change event, check if the entered value is a number. If it's not a number then set the old value back again.

    Dim textval As String
    Dim numval As String
    
    Private Sub TextBox1_Change()
      textval = TextBox1.Text
      If IsNumeric(textval) Then
        numval = textval
      Else
        TextBox1.Text = CStr(numval)
      End If
    End Sub
    
    0 讨论(0)
  • 2021-01-12 04:11

    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
    
    0 讨论(0)
  • 2021-01-12 04:12

    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
    
    0 讨论(0)
  • 2021-01-12 04:14

    Right click on control box > component > Control -> Microsoft Masked Edit Control 6.0.
    Or with normal textbox:

    Private Sub Text1_Validate(Cancel As Boolean)
     Cancel = Not IsNumeric(Text1.Text)
    
    End Sub
    
    0 讨论(0)
  • 2021-01-12 04:16

    Check this out:

    http://www.vbforums.com/showthread.php?t=350067

    You need to check each keypress, or you can do one validation at the end.

    0 讨论(0)
提交回复
热议问题