第一次发贴,就是想问问题,问题很幼稚,希望高手们赐教。
我想判断Textbox中内容的小数点位数,以便在输入数值时起到保护,比如有些参数要设为整数,有的要求带一位小数。这个代码要怎么写呢?我做了过下限的判断,但小数点就不知该如何弄了。
Private Sub Text1_Validate(Index As Integer, Cancel As Boolean)''Dim Dot As Long (我以为Dot就能判断,可是不行)If IsNumeric(Text1(Index).Text) = False Then
    Call ShowErrInfo("数据不正确,请修正!")
    Cancel = True
    Exit Sub
End If    If Val(Text1.Text) < 1 Then
        Call ShowErrInfo("循环个数设置不正确(下限值1),请修正!")
        Cancel = True
        Exit Sub
    End If期待回复。

解决方案 »

  1.   

    可否先转成字符串,然后split用字符串判断?Private Sub Command1_Click()
        Dim a As Double
        a = Text1.Text
        
        Dim b As String
        Dim c As String
        b = CStr(a)
        
        Dim sSplit() As String
        
        If InStr(1, b, ".", vbBinaryCompare) Then
            sSplit = Split(b, ".", -1)
            c = sSplit(1)
        End If
            
        Dim d As Single
        d = Len(c)
       
       MsgBox d
       
    End Sub
      

  2.   

    正则表达式.
    引用Microsoft VBScript Regular Expressions 5.5
    Dim result As Object
        Dim r      As RegExp
        Set r = New RegExp
        '匹配整数 -XXX ~ XXXX 此类
        r.Pattern = "^-{0,1}[1-9][0-9]{0,}$|^0$"
        r.IgnoreCase = True    If r.Test(Text1.Text) = True Then MsgBox ("符合要求")
        
        '匹配一位小数
        r.Pattern = "^-{0,1}[1-9][0-9]{0,}\.[0-9]$|^-{0,1}0\.[0-9]$"
        r.IgnoreCase = True
       If r.Test(Text1.Text) = True Then MsgBox ("符合要求")
      

  3.   


    Function Get_dLenth(ByVal s As String) As Integer
    Dim ss() As String
    If Not IsNumeric(s) Then
        Get_dLenth = -1
    Else
        ss = Split(s, ".")
        Get_dLenth = Len(ss(UBound(ss)))
    End If
    End Function
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then _
    Debug.Print Get_dLenth(Text1.Text)
    End Sub
      

  4.   

    上面的错了
    Function Get_dLenth(ByVal s As String) As Integer
    Dim ss() As String
    If Not IsNumeric(s) Then
        Get_dLenth = -1
    Else
        ss = Split(s, ".")
        If UBound(ss) = 0 Then
            Get_dLenth = 0
        Else
            Get_dLenth = Len(ss(UBound(ss)))
        End If
    End If
    End Function
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then _
    Debug.Print Get_dLenth(Text1.Text)
    End Sub
      

  5.   


    Private Sub Form_Load()
          Text1.Tag = 0
          Text1_LostFocus
    End SubPrivate Sub Text1_GotFocus()
          Text1.Text = Text1.Tag
          Text1.SelStart = 0
          Text1.SelLength = Len(Text1.Text)
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
          If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Or KeyAscii = 8 Or KeyAscii = Asc(".") Then
                If KeyAscii = Asc(".") And InStr(1, Text1.Text, ".", vbTextCompare) > 0 Then
                      KeyAscii = 0
                End If
                If Text1.SelStart >= Len(Text1.Text) - 2 And _
                      InStr(1, Text1.Text, ".", vbTextCompare) > 0 And _
                      Len(Text1.Text) - InStrRev(Text1.Text, ".", Len(Text1.Text), vbTextCompare) >= 2 And _
                      KeyAscii <> 8 Then
                       
                      KeyAscii = 0
                End If
          Else
                KeyAscii = 0
          End If
    End SubPrivate Sub Text1_LostFocus()
          Dim SaveNumer As Currency
          If IsNumeric(Text1.Text) = False Then
                SaveNumer = 0
          Else
                SaveNumer = Format(Text1.Text, "0.00")
          End If
          Text1.Tag = SaveNumer
          Text1.Text = Format(SaveNumer, "0.00")
    End Sub
      

  6.   

    在是否数值和下限判断通过后,直接用 Format() 函数进行格式化,即限定了精度也保证了显示的一致性。
    Private Sub Text1_Validate(Index As Integer, Cancel As Boolean)
        ...
        Text1 = Format$(Text1, "0") '<-限定为整数'
        Text1 = Format$(Text1, "0.0") '<-限定为1位小数'
    End Sub
      

  7.   

    用round函数判断一下即可:         Dim PAI As Double
            PAI = 3.14159265358979
            Debug.Print Round(PAI, 1)
            Debug.Print Round(PAI, 2)
            Debug.Print Round(PAI, 3)
            Debug.Print Round(PAI, 4)
            Debug.Print Round(PAI, 5)
            Debug.Print Round(PAI, 6)
            Debug.Print Round(PAI, 7)
            Debug.Print Round(PAI, 8)
            Debug.Print Round(PAI, 9)
            Debug.Print Round(PAI, 10)
            
            输出结果如下:
            3.14
            3.142
            3.1416
            3.14159
            3.141593
            3.1415927
            3.14159265
            3.141592654
            3.1415926536
      

  8.   

    是的,这样看起来很整齐,我是先统一指定了数值的类型,然后在"Tag"里面限定,如要改变只需修改"Tag".这样就避免逐个Text去改代码.
      

  9.   

    这个就为整数了:
    Text1 = Round(Text1.Text)