如何控制在一个文本框输入的数据类型不错,例如,我添加text1.text,然后我想在text1.text必须输入数字,而输入字符的时候不允许。

解决方案 »

  1.   

    '数值型判断:isnumeric(text1.text)
    '日期型判断:isdate(text1.text)private sub text1_lostfocus()
        if text1.text<>"" then
            if not isnumeric(text1.text) then
                msgbox "请输入数值型数据!",48,"提示"
                text1.setfocus
                exit sub
            end if
         end if
    end sub
      

  2.   

    Private Sub Text1_Change()
        If IsNumeric(Text1.Text) = False Then
            MsgBox "请输入数字"
        End If
    End Sub
      

  3.   

    Private Sub Text1_Validate(Cancel As Boolean)
        if text1.text<>"" then
            if not isnumeric(text1.text) then
                cancel=true
            end if
         end if
    End Sub
      

  4.   


    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
    End Sub

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case 48 To 57
            Case 8
            Case Else
            Beep
            MsgBox "error!"
            KeyAscii = 0
        End Select三
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
        Private Const ES_NUMBER = &H2000
        Private Const GWL_STYLE = -16Private Sub ForceNumeric(Box As TextBox)
        On Error Goto Catch
        Dim nStyle As Long
        nStyle = GetWindowLong(Box.hWnd, GWL_STYLE)
        Call SetWindowLong(Box.hWnd, GWL_STYLE, nStyle Or ES_NUMBER)
        Goto Finally
        Catch:
        Call MsgBox(Err.Description, vbCritical Or vbOKOnly, "Error")
        Finally:
    End Sub