|
以下的代码实现在 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 控件的应用--动态显示含有千分号的数字
|