找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 5073|回复: 3

Masked Edit 控件的应用--动态显示含有千分号的数字

[复制链接]

24

主题

37

回帖

3

精华

钻石会员

积分
4243
发表于 2006-4-2 20:44:01 | 显示全部楼层 |阅读模式
以下的代码实现在  Masked Edit 控件 中实现动态显示千分号的数字。因小弟水平有限,希望大家帮忙测试并精简、修正、改进代码,谢谢大家!
Dim i%
Dim MySelStart%
Dim MyKeyCode%
Dim Oldtext As String
Private Sub Form_Load()
    i = 2
    MaskEdBox3.Text = "0.00"
    MaskEdBox3.SelLength = 1
    MaskEdBox3.MaxLength = 15    '设置 Masked Edit 控件的最大长度(限制输入的数字只能去到亿位)
End Sub
Private Sub MaskEdBox3_Change()
Dim NewText As String
    i = i + 1
    If i Mod 2 = 0 Then    '防止连续引发 Change 事件 导致“堆栈空间溢出”
        MySelStart = MaskEdBox3.SelStart
        If InStr(MaskEdBox3.Text, ".") = 0 And Len(MaskEdBox3.Text) = 1 Then    '针对选定所有内容再输入数字的情况
            MaskEdBox3.Text = MaskEdBox3.Text & ".00"
            MaskEdBox3.SelStart = 1
            Exit Sub
        End If
        If MaskEdBox3.Text = "" Then    '针对删除所有内容的情况
            MaskEdBox3.Text = "0.00"
            Exit Sub
        End If
        If InStr(MaskEdBox3.Text, ".") <> 0 Then
            NewText = Replace(MaskEdBox3.Text, ",", "")    '去除分节号
        Else
            NewText = Left(MaskEdBox3.Text, Len(MaskEdBox3.Text) - 2) & "." & Right(MaskEdBox3.Text, 2)
            MySelStart = MySelStart + 1
        End If
        If Right(NewText, 4) = "0.00" And MaskEdBox3.SelStart = 1 Then    '针对内容为“0.00”且光标的位置为0时输入数字的情况
            MaskEdBox3.Text = Left(MaskEdBox3.Text, 1) & ".00"
        Else
            If (Len(NewText) - InStr(NewText, ".")) = 3 Then    '针对在小数点后面的数字的增、删情况
                If MySelStart = InStr(MaskEdBox3.Text, ".") + 1 Then
                    MaskEdBox3.Text = Format(Left(NewText, InStr(NewText, ".")) & Mid(NewText, InStr(NewText, ".") + 1, 1) & Mid(NewText, InStr(NewText, ".") + 3, 1), "#,##0.00")
                    MaskEdBox3.SelStart = MySelStart
                    Exit Sub
                Else
                    MaskEdBox3.Text = Format(Left(NewText, InStr(NewText, ".")) & Mid(NewText, InStr(NewText, ".") + 1, 1) & Mid(NewText, InStr(NewText, ".") + 2, 1), "#,##0.00")
                    MaskEdBox3.SelStart = MySelStart
                    Exit Sub
                End If
            Else
                MaskEdBox3.Text = Format(Left(NewText, InStr(NewText, ".") + 2), "#,##0.00")
            End If
        End If
        If MyKeyCode = 8 Then    '设置按下 Back Space 键后光标的位置
            Call ggg(NewText)
        ElseIf MyKeyCode = 46 Then    '设置按下 Delete 键后光标的位置
            If Oldtext = NewText Then    '针对光标在千分号或小数点前面的情况
                If MySelStart = InStr(MaskEdBox3.Text, ".") Then   '光标在小数点前面时
                    MaskEdBox3.SelStart = MySelStart
                    Exit Sub
                Else  '光标在千分号前面时
                    MaskEdBox3.SelStart = MySelStart + 1
                    Exit Sub
                End If
            ElseIf MySelStart = InStr(MaskEdBox3.Text, ".") Then
                MaskEdBox3.SelStart = MySelStart
                Exit Sub
            Else
                Call ggg(NewText)
            End If
        Else
            If Len(NewText) = 10 Or Len(NewText) = 7 Then    '针对当数值由百位变成千位或由十万位变成百万位的情况
                MaskEdBox3.SelStart = MySelStart + 1
            Else
                MaskEdBox3.SelStart = MySelStart
            End If
        End If
        Oldtext = Replace(MaskEdBox3.Text, ",", "")    '去除分节号
    Else
    End If
End Sub
Private Sub MaskEdBox3_KeyDown(KeyCode As Integer, Shift As Integer)
    MyKeyCode = KeyCode    '捕获 Back Space 键 和 Delete 键 的动作
End Sub
Private Sub MaskEdBox3_KeyPress(KeyAscii As Integer)
    If KeyAscii = 46 Then    '如果输入小数点,则光标跳到现有内容的小数点后面
        KeyAscii = 0
        MaskEdBox3.SelStart = Len(MaskEdBox3.Text) - 2
    End If
    If (KeyAscii < 48 And KeyAscii <> 46 And KeyAscii <> 8) Or KeyAscii > 58 Then    '限制只能输入数字和小数点和 Back Space 键 和 Delete 键
        KeyAscii = 0
    End If
End Sub
Sub ggg(NewText As String)
    If MySelStart <> 0 Then
        If Len(NewText) = 9 Or Len(NewText) = 6 Then    '针对当数值由千位变成百位或由百万位变成十万位的情况
            MaskEdBox3.SelStart = MySelStart - 1
        Else
            MaskEdBox3.SelStart = MySelStart
        End If
    Else
        MaskEdBox3.SelStart = 0
    End If
End Sub
3075z5gP.rar (3.13 KB, 下载次数: 9510)

Masked Edit 控件的应用--动态显示含有千分号的数字

Masked Edit 控件的应用--动态显示含有千分号的数字

275

主题

3017

回帖

1

精华

管理员

嗷嗷叫的老马

积分
17064

论坛牛人贡献奖关注奖最佳版主进步奖人气王疯狂作品奖精英奖赞助论坛勋章乐于助人勋章

QQ
发表于 2006-4-3 20:56:07 | 显示全部楼层
顶!!

人在网吧走,没有VB玩啊~~~~~[em06][em06][em06][em06]
我就是嗷嗷叫的老马了......

275

主题

3017

回帖

1

精华

管理员

嗷嗷叫的老马

积分
17064

论坛牛人贡献奖关注奖最佳版主进步奖人气王疯狂作品奖精英奖赞助论坛勋章乐于助人勋章

QQ
发表于 2006-4-6 11:04:05 | 显示全部楼层
我现在也是好忙,上网时间也不太多

这段时间的论坛就要靠大家了....

不过这个控件来做计算器倒是不错!
我就是嗷嗷叫的老马了......

24

主题

37

回帖

3

精华

钻石会员

积分
4243
 楼主| 发表于 2006-4-10 15:41:16 | 显示全部楼层
再次改进了一下,欢迎大家进行测试,谢谢大家! hdZ0dA5B.rar (10.98 KB, 下载次数: 9643)
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表