我正在尝试在表单加载和事件中将字符串格式化为文本框中的货币,并使用 VB.NET 支持单词中的值。
对于文本框
txtdownpayment
不是只读的。因此,请指导我,以便我可以使用正确的事件来格式化货币字符串。
但它不起作用,并且还使 ValueInWords 文本框
txtvalueinwords
不出现,所以我在表单加载中注释掉了代码。也许我的代码有问题。请指导我
谢谢
所以我想要想要的结果,因为我在
txtdownpayment
,txttotalinvoice
中标记了橙色
或
$ #,###.###
表格中的代码
Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Valuetotalinvoice As Double = 0
txttotalinvoice.Text = CType(CDbl(250000), String)
Dim totalinvoice As Double = CDbl(txttotalinvoice.Text)
Double.TryParse(CType(totalinvoice, String), Valuetotalinvoice)
Dim Valuedownpayment As Double = 0
Double.TryParse(txtdownpayment.Text, Valuedownpayment)
'Dim culture As CultureInfo = CultureInfo.GetCultureInfo("en-US")
'Dim numberFormat As NumberFormatInfo = CType(culture.NumberFormat.Clone(), NumberFormatInfo)
'numberFormat.CurrencySymbol = "$ "
'txttotalinvoice.Text = String.Format(numberFormat, "{0:C}", Value)
txttotalinvoice.ReadOnly = True
txtvalueinwords.ReadOnly = True
Dim UserInput As Double
If Double.TryParse(txttotalinvoice.Text, UserInput) Then
txtvalueinwords.Text = ConvertCurrencyToEnglish(CDbl(txttotalinvoice.Text) - CDbl(Valuedownpayment)) + "Dollar"
End If
End Sub
Private Sub txtdownpayment_KeyUp(sender As Object, e As KeyEventArgs) Handles txtdownpayment.KeyUp
Dim Valuedownpayment As Double = 0
Dim Valuetotalinvoice As Double = 0
Double.TryParse(txtdownpayment.Text, Valuedownpayment)
Double.TryParse(txttotalinvoice.Text, Valuetotalinvoice)
txtvalueinwords.Text = ConvertCurrencyToEnglish(CDbl(Valuetotalinvoice) - CDbl(Valuedownpayment)) + "Dollar"
Dim culture As CultureInfo = CultureInfo.GetCultureInfo("en-US")
Dim numberFormat As NumberFormatInfo = CType(culture.NumberFormat.Clone(), NumberFormatInfo)
numberFormat.CurrencySymbol = "$ "
lblbalance.Text = String.Format(numberFormat, "{0:C}", (CDbl(Valuetotalinvoice) - CDbl(Valuedownpayment)))
End Sub
End Class
模块中的代码
Module modvalueinwords
Public Function ConvertCurrencyToEnglish(ByVal MyNumber As Double) As String
Dim Temp As String
Dim Dollars, Cents As String
Dim DecimalPlace, Count As Integer
Dim Place(9) As String
Dim Numb As String
Place(2) = " Thousand " : Place(3) = " Million " : Place(4) = " Billion " : Place(5) = " Trillion "
' Convert Numb to a string, trimming extra spaces.
Numb = Trim(Str(MyNumber))
' Find decimal place.
DecimalPlace = InStr(Numb, ".")
' If we find decimal place...
If DecimalPlace > 0 Then
' Convert cents
Temp = Left(Mid(Numb, DecimalPlace + 1) & "00", 2)
Cents = ConvertTens(Temp)
' Strip off cents from remainder to convert.
Numb = Trim(Left(Numb, DecimalPlace - 1))
End If
Count = 1
Do While Numb <> ""
' Convert last 3 digits of Numb to English dollars.
Temp = ConvertHundreds(Right(Numb, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(Numb) > 3 Then
' Remove last 3 converted digits from Numb.
Numb = Left(Numb, Len(Numb) - 3)
Else
Numb = ""
End If
Count = Count + 1
Loop
' Clean up dollars.
Select Case Dollars
Case "" : Dollars = "No "
Case "One" : Dollars = "One"
Case Else : Dollars = Dollars & ""
End Select
' Clean up cents.
Select Case Cents
Case "" : Cents = ""
Case "One" : Cents = " And One Cent"
Case Else : Cents = " And Cents " & Cents & ""
End Select
ConvertCurrencyToEnglish = Dollars & Cents
End Function
Private Function ConvertHundreds(ByVal MyNumber As String) As String
Dim Result As String
' Exit if there is nothing to convert.
If Val(MyNumber) = 0 Then Exit Function
' Append leading zeros to number.
MyNumber = Right("000" & MyNumber, 3)
' Do we have a hundreds place digit to convert?
If Left(MyNumber, 1) <> "0" Then
Result = ConvertDigit(Left(MyNumber, 1)) & " Hundred "
End If
' Do we have a tens place digit to convert?
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & ConvertTens(Mid(MyNumber, 2))
Else
' If not, then convert the ones place digit.
Result = Result & ConvertDigit(Mid(MyNumber, 3))
End If
ConvertHundreds = Trim(Result)
End Function
Private Function ConvertTens(ByVal MyTens As String) As String
Dim Result As String
' Is value between 10 and 19?
If Val(Left(MyTens, 1)) = 1 Then
Select Case Val(MyTens)
Case 10 : Result = "Ten"
Case 11 : Result = "Eleven"
Case 12 : Result = "Twelve"
Case 13 : Result = "Thirteen"
Case 14 : Result = "Fourteen"
Case 15 : Result = "Fifteen"
Case 16 : Result = "Sixteen"
Case 17 : Result = "Seventeen"
Case 18 : Result = "Eighteen"
Case 19 : Result = "Nineteen"
Case Else
End Select
Else
' .. otherwise it's between 20 and 99.
Select Case Val(Left(MyTens, 1))
Case 2 : Result = "Twenty "
Case 3 : Result = "Thirty "
Case 4 : Result = "Forty "
Case 5 : Result = "Fifty "
Case 6 : Result = "Sixty "
Case 7 : Result = "Seventy "
Case 8 : Result = "Eighty "
Case 9 : Result = "Ninety "
Case Else
End Select
' Convert ones place digit.
Result = Result & ConvertDigit(Right(MyTens, 1))
End If
ConvertTens = Result
End Function
Private Function ConvertDigit(ByVal MyDigit As String) As String
Select Case Val(MyDigit)
Case 1 : ConvertDigit = "One"
Case 2 : ConvertDigit = "Two"
Case 3 : ConvertDigit = "Three"
Case 4 : ConvertDigit = "Four"
Case 5 : ConvertDigit = "Five"
Case 6 : ConvertDigit = "Six"
Case 7 : ConvertDigit = "Seven"
Case 8 : ConvertDigit = "Eight"
Case 9 : ConvertDigit = "Nine"
Case Else : ConvertDigit = ""
End Select
End Function
End Module
用户将输入类似
250000
的数字,当他退出文本框时,您希望文本更新为 $ 250,000.00
。这可以使用文本框的 LostFocus 事件 来实现。您将收到通知,用户已移至下一个文本框,此时您可以通过以下方式更新文本框值:
您可以在步骤 1 中使用 IsNumeric 来验证输入的值。
当用户返回文本框时,您想要删除格式。您可以在文本框的 GotFocus 事件 中执行此操作。您可以使用 String.Replace 手动删除
$
和 ,
字符,留下一个干净的字符串,您可以将其转换为 Int 并用于更新 TextBox.Text。