本人有个问题,现在要限定text文本框中输入的内容只能是数字,当光标不在该文本框中时检查输入的是否为数字,如不是用msgbox警告。请教代码怎么搞!!!!!

解决方案 »

  1.   

    Private Sub Text1_Change()
    If Not ValidateNumeric(Text1.Text) ThenText1.Text = ""End If
    End Sub
    Private Function ValidateNumeric(strText As String) As BooleanValidateNumeric = CBool(strText = "" Or strText = "-" Or strText = "+" Or strText = "." Or IsNumeric(strText))End Function
      

  2.   

    private sub text1_validate()
      if text1<>"" then
        if not isnumeric(text1) then  msgbox "不是数字"
      end if
    end sub
      

  3.   

    private sub text1_lostfocus()
        if text1.text<>"" then
            if not isnumeric(text1.text) then
                msgbox "请输入数值型数据!",48,"提示"
                text1.setfocus
            end if
        end if
    end sub
      

  4.   

    private sub text1_validate()
         if not isnumeric(text1.text)  then 
               cancel=true
               msgbox"请输入数字",,"数字"
         end if
    end sub
    数字那地方就是你text1前面label几的caption
      

  5.   

    我建议你在输入值的时候就判断输入的是否为数字,不必要在TextBox失去焦点的时候再判断是否为数字。对应的是它的KeyPress事件:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
            If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 13 Then KeyAscii = 0 
    End Sub
      

  6.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim Numbers As String
    Numbers = "1234567890" + Chr(8) + Chr(46)
    If InStr(Numbers, Chr(KeyAscii)) = 0 Then
        KeyAscii = 0
    End If
    End Sub