高手,在textbox中只能输入数字,包括小数点.
请问如何控制?

解决方案 »

  1.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If (KeyAscii = vbKeyBack) Then Exit Sub
        If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) And KeyAscii <> Asc(".") Then
            KeyAscii = 0
        End If
    End Sub
      

  2.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        InputNumeric KeyAscii, Text1
    End Sub'*****************************************************************
    '文本框只能输入数字Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox)
        Select Case KeyAscii
            Case Asc("-") '允许负数
                If txtItem.SelStart = 0 Then
                  If Left(txtItem.Text, 1) = "-" Then
                      KeyAscii = 0
                      Beep
                  End If
                Else
                  KeyAscii = 0
                  Beep
                End If
            Case 8
                  '无变化,退格键不屏蔽
            Case Asc(" ") '32
                If txtItem.SelLength = 0 Then
                    KeyAscii = 0
                End If
            Case Asc(".") '46 '允许小数点
                If InStr(txtItem.Text, ".") Then
                    KeyAscii = 0
                End If
            Case Is < Asc(0) '48
                  KeyAscii = 0
            Case Is > Asc(9) '57
                  KeyAscii = 0
        End Select
    End Sub
      

  3.   

    本人一段代码 希望大家帮忙测试代码如下:Dim Txt As String, Num As StringPrivate Sub Text1_Change()
      Num = Text1
      If Num = "" Or Num Like "[+-.0-9]" Or Len(Num) > 1 _
         And Not Mid$(Num, 2) Like "*[!.0-9]*" _
         And Not Num Like "*.*.*" _
         And Not Left(Num, 2) Like "0#" _
         And Not Left(Num, 3) Like "[+-]0#" Then
        Txt = Text1
        Exit Sub
      End If
      Text1 = Txt: Text1.SelStart = Len(Text1)
    End Sub
      

  4.   

    Private Sub Text1_Change()
     on error resume next
     dim s as string,i as long
     i=text1.selstart
     s=text1.text
     if isnumeric(s) then
      text1.tag=s
     else
      text1.text=text1.tag
      text1.selstart=i
     end if
    end sub
      

  5.   

    Option ExplicitPrivate Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = 8 Then Exit Sub
        If KeyAscii = 46 Then Exit Sub
        If IsNumeric(Chr(KeyAscii)) Then Exit Sub
        KeyAscii = 0
    End Sub
      

  6.   

    Option ExplicitPrivate Sub Text1_KeyPress(KeyAscii As Integer)
        If Len(Text1.Text) > 0 Then
            Select Case CStr(Left(Text1.Text, 1))
                Case "0"
                    While Left(Text1.Text, 1) = "0"
                        If Mid(Text1.Text, 2, 1) = "." Or Mid(Text1.Text, 2, 1) > 0 Then
                            Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
                        End If
                    Wend
                    Text1.Text = Text1.Text & Chr(KeyAscii)
                    SendKeys ("{End}")
                    KeyAscii = 0
                Case "-"
                    Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
                    While Left(Text1.Text, 1) = "0"
                        If Mid(Text1.Text, 2, 1) = "." Or Mid(Text1.Text, 2, 1) > 0 Then
                            Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
                        End If
                    Wend
                    Text1.Text = "-" & Text1.Text & Chr(KeyAscii)
                    SendKeys ("{End}")
                    KeyAscii = 0
                Case "."
                    Text1.Text = "0" & Text1.Text & Chr(KeyAscii)
                    SendKeys ("{End}")
                    KeyAscii = 0
            End Select
        End If
        
        If KeyAscii = 45 And Len(Text1.Text) = 0 Then Exit Sub
        If KeyAscii = 8 Then Exit Sub
        If KeyAscii = 46 And InStr(1, Text1.Text, ".") = 0 Then Exit Sub
        If IsNumeric(Chr(KeyAscii)) Then Exit Sub
        KeyAscii = 0
    End Sub
    ------------
    还有很多问题自己看看吧,我都晕了!
      

  7.   

    Public Function LrxsZxz(TextName As TextBox, AsciiX As Integer) As Integer '錄入小數正限制
        If Not ((Chr(AsciiX) >= "0" And Chr(AsciiX) <= "9") Or (Chr(AsciiX) = "." And InStr(1, TextName.Text, ".") = 0) _
           Or AsciiX = vbKeyBack) Then
            LrxsZxz = 0
        Else
            LrxsZxz = AsciiX
        End If
    End Function
      

  8.   

    Public Function LrxsZfxz(TextName As TextBox, AsciiX As Integer) As Integer '錄入小數正負限制
        If Not ((Chr(AsciiX) >= "0" And Chr(AsciiX) <= "9") Or (Chr(AsciiX) = "." And InStr(1, TextName.Text, ".") = 0) _
           Or AsciiX = vbKeyBack Or (Chr(AsciiX) = "-" And TextName.SelStart = 0)) Then
            LrxsZfxz = 0
        Else
            LrxsZfxz = AsciiX
        End If
    End FunctionPublic Function LrzsZxz(AsciiX As Integer) As Integer '錄入整數正限制
        If Not ((AsciiX >= Asc("0") And AsciiX <= Asc("9")) Or AsciiX = vbKeyBack) Then
            LrzsZxz = 0
        Else
            LrzsZxz = AsciiX
        End If
    End FunctionPublic Function Lrrqxz(AsciiX As Integer) As Integer '錄入日期限制
        If Not ((Chr(AsciiX) >= "0" And Chr(AsciiX) <= "9") Or Chr(AsciiX) = "-" Or AsciiX = vbKeyBack) Then
            Lrrqxz = 0
        Else
            Lrrqxz = AsciiX
        End If
    End Function