请问如何做到限制textbox中数字的小数位数,比如输入3位小数后,就不能输入小数了?谢谢

解决方案 »

  1.   

    If Left(Right(Text1.Text, 4), 1) = "." Then MsgBox "三位小数了"
      

  2.   

    比较笨的方法:
    Dim str_prvStr As String
    Private Sub Form_Load()
        Text1.Text = ""
    End SubPrivate Sub Text1_Change()
        Dim x() As String
        
        If (IsNumeric(Text1.Text)) Then
            x = Split(Text1.Text, ".")
            If (UBound(x) >= 1) Then
                If (Len(x(UBound(x))) > 3) Then
                    Text1.Text = str_prvStr
                    Text1.SelStart = Len(Text1.Text)
                End If
            End If
        End If
        str_prvStr = Text1.Text
    End Sub
      

  3.   

    Option ExplicitPrivate Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = 45 And Len(Text1.Text) = 0 Then Exit Sub
        If KeyAscii = 8 Then Exit Sub
        If KeyAscii = 46 And Len(Text1.Text) >= 3 Then KeyAscii = 0
        If KeyAscii = 46 And InStr(1, Text1.Text, ".") = 0 Then Exit Sub
        If IsNumeric(Chr(KeyAscii)) Then Exit Sub
        KeyAscii = 0
    End Sub
      

  4.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
      s = InStr(Text1, ".")
      If s And Len(Mid(Text1, s + 1)) > 2 Then KeyAscii = 0
    End Sub
      

  5.   

    此题并不简单。各位可考虑过用户输入小数点后又移动插入点补写或改写整数部分的情况?Private Sub Text1_Change()
    Dim n As Integer, s As Integer
        n = InStr(Text1, ".")
        s = Text1.SelStart
        If n >= 1 Then
            Text1 = Left(Text1, n + 3)
            If s > Len(Text1) Then
                Text1.SelStart = Len(Text1)
            Else
                Text1.SelStart = s
            End If
        End If
        
    End Sub