请教各位高手,怎么让textbox输入数字时,小数点后输入2位后,再输入就无响应.    就这点分了,不好意识

解决方案 »

  1.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If InStr(1, Text1.Text, ".") = 0 Then
            Select Case KeyAscii
                Case 48 To 57
                Case Asc(".")
                    If InStr(1, Text1.Text, ".") Then KeyAscii = 0
                Case 8
                Case Else
                    KeyAscii = 0
            End Select
        ElseIf Text1.SelStart >= InStr(1, Text1.Text, ".") Then
            If Len(Text1.Text) - InStr(1, Text1.Text, ".") >= 2 Then
                If KeyAscii <> 8 Then KeyAscii = 0
            Else
                Select Case KeyAscii
                    Case 48 To 57
                    Case 8
                    Case Else
                        KeyAscii = 0
                End Select
            End If
        End If
    End Sub呵呵,代码罗索了一点
      

  2.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Dim n As Long
        Dim s As String
        
        s = Text1.Text
        
        Select Case KeyAscii
            Case 8
            
            Case 45 '"-"
                If Len(s) > 0 Then KeyAscii = 0
            Case 46 '"."
                n = InStr(s, ".")
                If n > 0 Then KeyAscii = 0
            Case 48 To 57 '"0-9"
                n = InStr(s, ".")
                If n > 0 And n = Len(s) - 2 Then
                    If Text1.SelStart > n And Text1.SelLength = 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case Else
                KeyAscii = 0
        End Select
    End Sub
    补充一下,顺便蹭点分,哈哈
      

  3.   

    Public Sub ControlInput(txtText As TextBox, IntegerDigits As Integer, DecimalDigits As Integer)
        'txtText 为当前操作的文本框,IntegerDigits 为要限制输入的整数部分的位数 ,DecimalDigits 为要限制输入的小数部分的位数
        Dim txtCurrently As String                              '当前的文本内容
        Dim Str_IntegerDigits                                   '整数部分
        Dim Digits As String                                    '位数
        Dim Bln_Negative As Boolean                             '是否为负数
        Dim userKeyAsc As Integer
        
        txtCurrently = txtText.SelStart
        Bln_Negative = False
       
        Digits = InStr(1, txtText, "-")
        If Digits > 0 Then txtText = Mid(txtText, Digits)
        
        If Left(txtText, 1) = "-" Then
            Bln_Negative = True
            IntegerDigits = IntegerDigits - 1
            Str_IntegerDigits = Mid(txtText, 2)
        Else
            Str_IntegerDigits = Mid(txtText, 1)
        End If
       
        Digits = InStr(1, Str_IntegerDigits, ".")
       
        If Digits > 0 Then
            If IntegerDigits > Digits - 1 Then
                Str_IntegerDigits = Mid(Str_IntegerDigits, 1, Digits - 1) + Mid(Str_IntegerDigits, Digits, DecimalDigits + 1)
            Else
                IntegerDigits = Mid(Str_IntegerDigits, 1, IntegerDigits) + Mid(Str_IntegerDigits, Digits, DecimalDigits + 1)
                Digits = InStr(1, Str_IntegerDigits, ".")
            End If
            Digits = Len(Str_IntegerDigits) - Digits
            If Left(Str_IntegerDigits, 1) = "." Then
                txtCurrently = txtCurrently + 1
                Str_IntegerDigits = "0" & Str_IntegerDigits
            End If
            If Digits < DecimalDigits Then
               Str_IntegerDigits = Format(Str_IntegerDigits, "#0." + String(Digits, "0"))
            Else
               Str_IntegerDigits = Format(Str_IntegerDigits, "#0." + String(DecimalDigits, "0"))
            End If
        Else
            Str_IntegerDigits = Mid(Str_IntegerDigits, 1, IntegerDigits)
            Str_IntegerDigits = Format(Str_IntegerDigits)
        End If
        
        If Bln_Negative Then Str_IntegerDigits = "-" & Str_IntegerDigits
        txtText = Str_IntegerDigits
        txtText.SelStart = txtCurrentlyEnd Sub然后在Text 的Change事件中调用些方法,格式如下:
    如果规定只能录入两位整数与两位小数,则如下Private Sub Text1_Change()
        Call ControlInput(Text1, 2, 2)
    End Sub
      

  4.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Dim n As Long
        Dim s As String
        
        s = Text1.Text
        
        Select Case KeyAscii
            Case 8
            
            Case 45 '"-"
                If Len(s) > 0 Then KeyAscii = 0
            Case 46 '"."
                n = InStr(s, ".")
                If n > 0 Then KeyAscii = 0
            Case 48 To 57 '"0-9"
                n = InStr(s, ".")
                If n > 0 And n = Len(s) - 2 Then
                    If Text1.SelStart >= n And Text1.SelLength = 0 Then
                        KeyAscii = 0
                    End If
                End If
            Case Else
                KeyAscii = 0
        End Select
    End Sub更正:If Text1.SelStart > n And Text1.SelLength = 0 Then
         If Text1.SelStart >= n And Text1.SelLength = 0 Then 打漏了"="