请问如何限制文本框中输入的数字整数位数和小数位数?

解决方案 »

  1.   

    Option Explicit
    Const MM = 8   '整数位长度
    Const NN = 3   '小数位长度
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Static M As Integer    '记录整数位长度
        Static N As Integer    '记录小数位长度
        
        Static Mytag As Boolean    '是否已经存在小数点
       If (KeyAscii <= 57 And KeyAscii >= 48) Or KeyAscii = 43 Or KeyAscii = 45 _
                                 Or KeyAscii = 46 Or KeyAscii = vbKeyBack Then  
        If Not Mytag Then
            If M = 8 Then
                Text1.Text = Text1.Text & "."
                Text1.SelStart = 9
                Mytag = True
                N = 1
            Else
                If KeyAscii <> 46 Then
                    M = M + 1
                Else
                    Mytag = True
                   
                End If
            End If
        Else
            If N = NN Then
                Beep
                KeyAscii = 0
            Else
                If KeyAscii = 46 Then
                    Beep
                    KeyAscii = 0
                Else
                    N = N + 1
                End If
            End If
        End If
        Else
            Beep
            KeyAscii = 0
        End If
        
    End Sub
    我自己写好了以后试过能用,只接受数字小数点正负号和退格,对于退格和正负号的输入没加处理,我想你也用不到(主要原因是来不及了,有个MM在等我:))
    有兴趣的话,加我QQ23011619,我再帮你加进去
    哎,你才给10分,难怪没人回你,别人限制只接受数字就是给好几十的哦
    呵呵,都是菜鸟相互学习