7,763
社区成员
发帖
与我相关
我的任务
分享
Private Sub Text1_Change()
setFormat Text1
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii > 48 And KeyAscii < 57) Or KeyAscii = 8 Or KeyAscii = 46 Or ((KeyAscii = 43 Or KeyAscii = 45) And Text1.SelStart = 0)) Then KeyAscii = 0
End Sub
Private Sub setFormat(objTxt As TextBox)
Dim v, strNew$, s1$, intSelStart%
Dim reg As Object
Set reg = CreateObject("vbscript.regExp")
reg.Global = True
If objTxt.Text = "" Then Exit Sub
v = Split(objTxt.Text, ".")
s1 = v(0)
v(0) = Replace(v(0), ",", "")
reg.Pattern = "(\d{3})"
v(0) = StrReverse(reg.Replace(StrReverse(v(0)), "$1,")) '每隔3数字加逗号
reg.Pattern = "^([^\d]*?),"
v(0) = reg.Replace(v(0), "$1")
intSelStart = objTxt.SelStart
objTxt.Text = Join(v, ".")
objTxt.SelStart = intSelStart + Len(v(0)) - Len(s1)
End Sub
怎么看还是我的代码最精简。。。
Private Sub Text1_Change()
Static LastText As String
Dim Str1 As String, Str2 As String, Str As String
Str = Replace(Text1.Text, ",", "")
If IsNumeric(Str & "0") And InStr(LCase(Str), "e") = 0 And InStr(LCase(Str), "d") = 0 Then
If IsNumeric(Str) Then
If InStr(Str, ".") Then
Str1 = Mid(Str, 1, InStr(Str, "."))
Str2 = Mid(Str, InStr(Str, "."))
Else
Str1 = Str
End If
Text1.Text = IIf(Left(Str1, 1) = "+", "+", "") & Format(Str1, "#,#") & Str2
Text1.SelStart = Len(Text1.Text)
End If
LastText = Str
Else
Text1.Text = LastText
Text1.SelStart = Len(Text1.Text)
End If
End Sub
Private Sub Text1_Change()
Static LastText As String
Dim Str1 As String, Str2 As String
If IsNumeric(Text1.Text & "0") And InStr(LCase(Text1.Text), "e") = 0 And InStr(LCase(Text1.Text), "d") = 0 Then
If IsNumeric(Text1.Text) Then
If InStr(Text1.Text, ".") Then
Str1 = Mid(Text1.Text, 1, InStr(Text1.Text, "."))
Str2 = Mid(Text1.Text, InStr(Text1.Text, "."))
Else
Str1 = Text1.Text
End If
Text1.Text = IIf(Left(Str1, 1) = "+", "+", "") & Format(Str1, "#,#") & Str2
Text1.SelStart = Len(Text1.Text)
End If
LastText = Text1.Text
Else
Text1.Text = LastText
End If
End Sub
Private Sub Text1_Change()
Static LastText As String
Dim Str1 As String, Str2 As String
If IsNumeric(Text1.Text & "0") Then
If IsNumeric(Text1.Text) Then
If InStr(Text1.Text, ".") Then
Str1 = Mid(Text1.Text, 1, InStr(Text1.Text, "."))
Str2 = Mid(Text1.Text, InStr(Text1.Text, "."))
Else
Str1 = Text1.Text
End If
Text1.Text = IIf(Left(Str1, 1) = "+", "+", "") & Format(Str1, "#,#") & Str2
Text1.SelStart = Len(Text1.Text)
End If
LastText = Text1.Text
Else
Text1.Text = LastText
End If
End Sub
If objTxt.Text = "" Then Exit Sub
Private Sub Text1_Change()
setFormat Text1
End Sub
'传入需要设置的文本框的name即可
Private Sub setFormat(objTxt As TextBox)
Dim v, s1$, s1_new$, s2$, intSelStart%
v = Split(objTxt.Text, ".")
s1 = Replace(v(0), ",", "")
If UBound(v) > 0 Then s2 = "." & v(1)
Dim reg As Object
Set reg = CreateObject("vbscript.regExp")
reg.Global = True
reg.Pattern = "(\d{3})"
s1_new = StrReverse(reg.Replace(StrReverse(s1), "$1,"))
If Left(s1_new, 1) = "," Then s1_new = Mid(s1_new, 2)
intSelStart = objTxt.SelStart
objTxt.Text = s1_new & s2
objTxt.SelStart = intSelStart + Len(s1_new) - Len(v(0)) ' Len(objTxt.Text)
End Sub
中午抽空写了下,重点还是正则哈
Option Explicit
Private Sub Text1_Change()
Text1Change Text1
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 8, 46
Case 43, 45 '+/- 正负号只能在第1位
If Text1.SelStart > 0 Then KeyAscii = 0
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Text1Change(bText As TextBox)
With bText
If Not IsNumeric(.Text) Then Exit Sub
Dim iPos As Integer, w1 As String
Dim pT As String, i As Integer
iPos = .SelStart '''记录光标位置
i = InStr(iPos + 1, .Text, ",") ''找到光标后面的,
If i = 0 Then i = InStr(.Text, ".") ''小数点的位置
If i = 0 Then i = Len(.Text) + 1 '''整个字符串
pT = Left$(.Text, i - 1) '''得到可能发生变化的部分
w1 = Format$(Replace(pT, ",", ""), "##,###")
If Left$(pT, 1) = "+" Then w1 = "+" & w1 '''补充+(在格式化时会丢掉的)
.Text = w1 & Right$(.Text, Len(.Text) - i + 1) '''更新格式化后的内容
.SelStart = iPos + Len(w1) - Len(pT) ''计算新光标位置
End With
End Sub
'
..Private Sub Text1_Change()
setFormat Text1
End Sub
'传入需要设置的文本框的name即可
Private Sub setFormat(objTxt As TextBox)
Dim v, strNew$, intLenDel%, s1$, s1_new$, s2$, intSelStart%
Dim reg As Object
Set reg = CreateObject("vbscript.regExp")
reg.Global = True
reg.Pattern = "[^\d\.+-.]" '匹配合法之外的字符的表达式删除掉
strNew = reg.Replace(objTxt.Text, "")
If strNew = "" Then
objTxt.Text = ""
Exit Sub
End If
v = Split(strNew, ".")
intLenDel = Len(objTxt.Text) - Len(strNew)
s1 = Replace(v(0), ",", "")
If UBound(v) > 0 Then s2 = "." & v(1)
reg.Pattern = "(\d{3})"
s1_new = StrReverse(reg.Replace(StrReverse(s1), "$1,")) '每隔3数字加逗号
reg.Pattern = "(\+|-),"
s1_new = reg.Replace(s1_new, "$1")
If Left(s1_new, 1) = "," Then s1_new = Mid(s1_new, 2)
intSelStart = objTxt.SelStart
objTxt.Text = s1_new & s2
objTxt.SelStart = intSelStart + Len(s1_new) - Len(v(0)) - intLenDel
End Sub
好啦,打了补丁了,正负这么变态的都能处理了。非法字符也都一句话删除了。