企鹅http://blog.yesky.com/Blog/penguinworkroom/复制地址
控制面板
日历
<2008年9月>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
留言簿(5)
文章分类
文章档案

'==================================================================
'           限制文本框只能输入0~99.99之间的数字
'                                       企鹅     2005-05-26
'==================================================================

'在删除小数点后出现3位以上连续数字的情况时将DELETE屏蔽掉
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
On Error Resume Next
    If KeyCode = 46 And ((Mid(Me.Text1.Text, Me.Text1.SelStart + 1, 1) = ".") And Len(Me.Text1.Text) > 3) Then
        KeyCode = 0
    End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
On Error Resume Next
    '限制只能输入0~9,小数点和Backspace
    Select Case KeyAscii
    Case 48 To 57, 46, 8
    Case Else
        KeyAscii = 0
        Exit Sub
    End Select
   
    '判断是否存在小数点
    If cz(Me.Text1.Text) = True Then
        Select Case KeyAscii
        '只能有一个小数点
        Case 46
            KeyAscii = 0
            Exit Sub
        '输入数字时
        Case 48 To 57
            '判断光标是否在小数点前
            If pdx(Me.Text1.Text, Me.Text1.SelStart) = True Then
                '判断小数点之前数字是否为连续2位
                If pdnum1(Me.Text1.Text) = True Then
                    KeyAscii = 0
                End If
            Else
                '判断小数点后数字是否为连续2位
                If pdnum2(Me.Text1.Text) = True Then
                    KeyAscii = 0
                End If
            End If
        '输入Backspace时
        Case 8
            '判断光标位置是否在小数后一位,及不让将小数点删除后出现三位连续数字
            If (Mid(Me.Text1.Text, Me.Text1.SelStart, 1) = ".") And Len(Me.Text1.Text) > 3 Then
                KeyAscii = 0
            End If
        End Select
    Else
        '输入是否为小数点和Backspace
        If KeyAscii <> 46 And KeyAscii <> 8 Then
            '判断小数点前2位是否为连续数字
            If pdnum1(Me.Text1.Text) = True Then
                KeyAscii = 0
            End If
        End If
    End If
   
End Sub

'查找字符串中是否有小数点
Function cz(chrstr As String) As Boolean
    For i = 1 To Len(chrstr)
        If Mid(chrstr, i, 1) = "." Then
            cz = True
            Exit Function
        End If
    Next i
    cz = False
End Function

'判断小数点前是否2位数字连续
Function pdnum1(chrstr As String) As Boolean
    Select Case Len(chrstr)
    Case 0, 1
    Case Else
        If (Mid(chrstr, 1, 1) >= "0" And Mid(chrstr, 1, 1) <= "9") And (Mid(chrstr, 2, 1) >= "0" And Mid(chrstr, 2, 1) <= "9") Then
            pdnum1 = True
            Exit Function
        Else
            pdnum1 = False
        End If
    End Select
End Function

'判断小数点后是否2位数字连续
Function pdnum2(chrstr As String) As Boolean
    Dim y As Integer
    For i = 1 To Len(chrstr)
        If Mid(chrstr, i, 1) = "." Then
            y = i
            Exit For
        Else
            y = i
        End If
    Next i
    If Len(chrstr) - y = 2 Then
        Select Case Len(chrstr)
        Case 0, 1
        Case Else
            For i = Len(chrstr) To y Step -1
                If (Mid(chrstr, i, 1) >= "0" And Mid(chrstr, i, 1) <= "9") And (Mid(chrstr, i - 1, 1) >= "0" And Mid(chrstr, i - 1, 1) <= "9") Then
                    pdnum2 = True
                    Exit Function
                End If
            Next i
            pdnum2 = False
        End Select
    Else
        pdnum2 = False
    End If
End Function

'判断光标位置是否在小数点之前
Function pdx(chrstr As String, x As Integer) As Boolean
    Dim y As Integer
    For i = 1 To Len(chrstr)
        If Mid(chrstr, i, 1) = "." Then
            y = i
            Exit For
        Else
            y = i
        End If
    Next i
    If x < y Then
        pdx = True
    Else
        pdx = False
    End If
End Function

 


作者:企鹅 阅读() 评论()  编辑 发表于:2005-05-26 16:03
相关内容
文章评论

暂无人对此文章发表评论!

发表评论
标题 *  
姓名 *  
内容 *  
   验证码: *       
       
版权声明:天极是本Blog托管服务提供商。如本文牵涉版权问题,天极不承担相关责任,请版权拥有者直接与文章作者联系解决。
Powered by:

Copyright © 企鹅