用TEXTBOX吧!在LOSTFOCUS事件里进行格式控制,如下:
Option ExplicitPrivate Sub txtNumber_GotFocus()
' filter out non-digit chars and unsignificant traling zeroes
On Error Resume Next
txtNumber.Text = FilterNumber(txtNumber.Text, True)
End SubPrivate Sub txtNumber_LostFocus()
' format as a number , grouping thousand digits
On Error Resume Next
If Int(CDbl(txtNumber.Text)) = CDbl(txtNumber.Text) Then
txtNumber.Text = Format(CDbl(txtNumber.Text), "#,###,###,##0")
Else
txtNumber.Text = Format(CDbl(txtNumber.Text), "#,###,###,##0.###")
End If
End SubPrivate Sub txtCurrency_GotFocus()
' filter out non-digit chars and unsignificant traling zeroes
' restore standard text color
On Error Resume Next
txtCurrency.Text = FilterNumber(txtCurrency.Text, True)
txtCurrency.ForeColor = vbWindowText
End SubPrivate Sub txtCurrency_LostFocus()
On Error Resume Next
' show negative values as red text
If CDbl(txtCurrency.Text) < 0 Then
txtCurrency.ForeColor = vbRed
End If
' format currency, but don't use parentesis for negative number
' (FormatCurrency is a new VB6 string function)
txtCurrency.Text = FormatCurrency(txtCurrency.Text, , , vbFalse)
End SubPrivate Sub txtDate_GotFocus()
' prepare to edit in short-date format
On Error Resume Next
txtDate.Text = Format$(CDate(txtDate.Text), "short date")
End SubPrivate Sub txtDate_LostFocus()
' convert to long-date format on exit
On Error Resume Next
txtDate.Text = Format$(CDate(txtDate.Text), "d MMMM yyyy")
End SubPrivate Sub txtPhone_GotFocus()
' trim embedded dashes
txtPhone.Text = FilterString(txtPhone.Text, "0123456789")
End SubPrivate Sub txtPhone_LostFocus()
' add dashes if necessary
txtPhone.Text = FormatPhoneNumber(txtPhone.Text)
End SubPrivate Sub txtCreditCard_GotFocus()
' trim embedded spaces
txtCreditCard.Text = FilterNumber(txtCreditCard.Text, True)
End SubPrivate Sub txtCreditCard_LostFocus()
' add spaces if necessary
txtCreditCard.Text = FormatCreditCard(txtCreditCard.Text)
End Sub
' filter out invalid characters
' (this function is used by all subsequent ones)Function FilterString(Text As String, validChars As String) As String
Dim i As Long, result As String
For i = 1 To Len(Text)
If InStr(validChars, Mid$(Text, i, 1)) Then
result = result & Mid$(Text, i, 1)
End If
Next
FilterString = result
End Function' filter out all non-numeric digits
' optionally trims non-significant trailing zeroesFunction FilterNumber(Text As String, TrimDecimal As Boolean) As String
Dim decSep As String, i As Long
Dim result As String
' retrieve the decimal separator symbol
decSep = Format$(0.1, ".")
' use FilterString for most of the work
result = FilterString(Text, decSep & "-0123456789")
' only if there is a decimal part and the
' user requested to trim non-significant digits
If TrimDecimal And InStr(Text, decSep) > 0 Then
For i = Len(result) To 1 Step -1
Select Case Mid$(result, i, 1)
Case decSep
result = Left$(result, i - 1)
Exit For
Case "0"
result = Left$(result, i - 1)
Case Else
Exit For
End Select
Next
End If
FilterNumber = resultEnd Function' format a phone number
' NOTE: this routine is US-specificFunction FormatPhoneNumber(Text As String) As String
Dim tmp As String
If Text <> "" Then
' first, get rid of all embedded dashes,if any
tmp = FilterString(Text, "0123456789")
' then, re-insert them in the correct position
If Len(tmp) <= 7 Then
FormatPhoneNumber = Left$(tmp, 3) & "-" & Mid$(tmp, 4)
Else
FormatPhoneNumber = Left$(tmp, 3) & "-" & Mid$(tmp, 4, 3) & "-" & Mid$(tmp, 7)
End If
End If
End Function' format a credit card numberFunction FormatCreditCard(Text As String) As String
Dim tmp As String
If Text <> "" Then
' first, get rid of all embedded spaces,if any
tmp = FilterNumber(Text, False)
' then, re-insert them in the correct position
FormatCreditCard = Left$(tmp, 4) & " " & Mid$(tmp, 5, 4) & " " & Mid$(tmp, 9, 4) & " " & Mid$(tmp, 13, 4)
End If
End Function
Option ExplicitPrivate Sub txtNumber_GotFocus()
' filter out non-digit chars and unsignificant traling zeroes
On Error Resume Next
txtNumber.Text = FilterNumber(txtNumber.Text, True)
End SubPrivate Sub txtNumber_LostFocus()
' format as a number , grouping thousand digits
On Error Resume Next
If Int(CDbl(txtNumber.Text)) = CDbl(txtNumber.Text) Then
txtNumber.Text = Format(CDbl(txtNumber.Text), "#,###,###,##0")
Else
txtNumber.Text = Format(CDbl(txtNumber.Text), "#,###,###,##0.###")
End If
End SubPrivate Sub txtCurrency_GotFocus()
' filter out non-digit chars and unsignificant traling zeroes
' restore standard text color
On Error Resume Next
txtCurrency.Text = FilterNumber(txtCurrency.Text, True)
txtCurrency.ForeColor = vbWindowText
End SubPrivate Sub txtCurrency_LostFocus()
On Error Resume Next
' show negative values as red text
If CDbl(txtCurrency.Text) < 0 Then
txtCurrency.ForeColor = vbRed
End If
' format currency, but don't use parentesis for negative number
' (FormatCurrency is a new VB6 string function)
txtCurrency.Text = FormatCurrency(txtCurrency.Text, , , vbFalse)
End SubPrivate Sub txtDate_GotFocus()
' prepare to edit in short-date format
On Error Resume Next
txtDate.Text = Format$(CDate(txtDate.Text), "short date")
End SubPrivate Sub txtDate_LostFocus()
' convert to long-date format on exit
On Error Resume Next
txtDate.Text = Format$(CDate(txtDate.Text), "d MMMM yyyy")
End SubPrivate Sub txtPhone_GotFocus()
' trim embedded dashes
txtPhone.Text = FilterString(txtPhone.Text, "0123456789")
End SubPrivate Sub txtPhone_LostFocus()
' add dashes if necessary
txtPhone.Text = FormatPhoneNumber(txtPhone.Text)
End SubPrivate Sub txtCreditCard_GotFocus()
' trim embedded spaces
txtCreditCard.Text = FilterNumber(txtCreditCard.Text, True)
End SubPrivate Sub txtCreditCard_LostFocus()
' add spaces if necessary
txtCreditCard.Text = FormatCreditCard(txtCreditCard.Text)
End Sub
' filter out invalid characters
' (this function is used by all subsequent ones)Function FilterString(Text As String, validChars As String) As String
Dim i As Long, result As String
For i = 1 To Len(Text)
If InStr(validChars, Mid$(Text, i, 1)) Then
result = result & Mid$(Text, i, 1)
End If
Next
FilterString = result
End Function' filter out all non-numeric digits
' optionally trims non-significant trailing zeroesFunction FilterNumber(Text As String, TrimDecimal As Boolean) As String
Dim decSep As String, i As Long
Dim result As String
' retrieve the decimal separator symbol
decSep = Format$(0.1, ".")
' use FilterString for most of the work
result = FilterString(Text, decSep & "-0123456789")
' only if there is a decimal part and the
' user requested to trim non-significant digits
If TrimDecimal And InStr(Text, decSep) > 0 Then
For i = Len(result) To 1 Step -1
Select Case Mid$(result, i, 1)
Case decSep
result = Left$(result, i - 1)
Exit For
Case "0"
result = Left$(result, i - 1)
Case Else
Exit For
End Select
Next
End If
FilterNumber = resultEnd Function' format a phone number
' NOTE: this routine is US-specificFunction FormatPhoneNumber(Text As String) As String
Dim tmp As String
If Text <> "" Then
' first, get rid of all embedded dashes,if any
tmp = FilterString(Text, "0123456789")
' then, re-insert them in the correct position
If Len(tmp) <= 7 Then
FormatPhoneNumber = Left$(tmp, 3) & "-" & Mid$(tmp, 4)
Else
FormatPhoneNumber = Left$(tmp, 3) & "-" & Mid$(tmp, 4, 3) & "-" & Mid$(tmp, 7)
End If
End If
End Function' format a credit card numberFunction FormatCreditCard(Text As String) As String
Dim tmp As String
If Text <> "" Then
' first, get rid of all embedded spaces,if any
tmp = FilterNumber(Text, False)
' then, re-insert them in the correct position
FormatCreditCard = Left$(tmp, 4) & " " & Mid$(tmp, 5, 4) & " " & Mid$(tmp, 9, 4) & " " & Mid$(tmp, 13, 4)
End If
End Function
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货