vb.netformattextboxcurrencycurrency-formatting

Formatting string into currency in textbox in form load and event and support Value In Words with VB.NET


I'm Trying to Formatting string into currency in textbox in form load and event and support Value In Words with VB.NET.

for textbox txtdownpayment is not readonly . So please guide me so that I can use the correct event in formatting the currency string.

but it didn't work and also made ValueInWords textbox txtvalueinwords not appear so I comment out code in the form load. Maybe something is wrong with my code. Please guide me

Thanks

So I want the desired result as I marked the orange color in txtdownpayment , txttotalinvoice

desired result

or

$ #,###.###

Code in form

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

Code in module

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

result code in gif

Update code based on according to the guidelines and recommendations and update function from @ÉtienneLaneville

Public Class Form3
    Public Function GetCurrencyString(currency As Single) As String
        Dim culture As CultureInfo = CultureInfo.GetCultureInfo("en-US")
        Dim numberFormat As NumberFormatInfo = CType(culture.NumberFormat.Clone(), NumberFormatInfo)
        numberFormat.CurrencySymbol = "$ "
        Return String.Format(numberFormat, "{0:C}", currency)
    End Function
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Valuetotalinvoice As Double = 0
        Dim Valuedownpayment As Double = 0
        Double.TryParse(txttotalinvoice.Text, Valuetotalinvoice)
        Double.TryParse(txtdownpayment.Text, Valuedownpayment)
        Double.TryParse(txtvalueinwords.Text, Valuetotalinvoice)
        txttotalinvoice.Text = GetCurrencyString(CSng(250000))
        txttotalinvoice.ReadOnly = True
        txtvalueinwords.ReadOnly = True
        Dim UserInput As Double
        If Double.TryParse(txttotalinvoice.Text, UserInput) Then
            txtvalueinwords.Text = ConvertCurrencyToEnglish(CSng(txttotalinvoice.Text) - CSng(Valuedownpayment)) + "Dollar"
        End If
    End Sub
    Private Sub txtdownpayment_KeyUp(sender As Object, e As KeyEventArgs) Handles txtdownpayment.KeyUp
        txttotalinvoice.Text = txttotalinvoice.Text.Replace(",", "").Replace("$ ", "").Replace(".00", "")
        Dim Valuedownpayment As Double = 0
        Dim Valuetotalinvoice As Double = 0
        Double.TryParse(txtdownpayment.Text, Valuedownpayment)
        Double.TryParse(txttotalinvoice.Text, Valuetotalinvoice)
        txtvalueinwords.Text = ConvertCurrencyToEnglish(CSng(Valuetotalinvoice) - CSng(Valuedownpayment)) + "Dollar"
        lblbalance.Text = GetCurrencyString(CSng(Valuetotalinvoice) - CSng(Valuedownpayment))
        txttotalinvoice.Text = GetCurrencyString(CSng(Valuetotalinvoice))
    End Sub
    Private Sub txtdownpayment_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtdownpayment.LostFocus
        Dim downpayment As Double = 0
        Double.TryParse(txtdownpayment.Text, downpayment)
        txtdownpayment.Text = GetCurrencyString(CSng(downpayment))
    End Sub
    Private Sub txtdownpayment_GotFocus(sender As Object, e As EventArgs) Handles txtdownpayment.GotFocus
        txtdownpayment.Text = txtdownpayment.Text.Replace(",", "").Replace("$ ", "").Replace(".00", "")
    End Sub
    Private Sub OnText_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtdownpayment.KeyPress
        If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub
End Class


result after update code


Solution

  • The user will type in a number like 250000 and as he exits the TextBox, you want the text to update to $ 250,000.00. This can be achieved using the TextBox's LostFocus event. You will be notified that the user has moved on to the next TextBox and, at that point, you can update the TextBox value by:

    1. Converting the Text from the TextBox to a Single using CSng().
    2. Use this Single value to update your balance.
    3. Use String.Format to convert the Single value to a String.
    4. Set the TextBox's Text value to the String.

    You can use IsNumeric in Step 1 to validate the value entered.

    When the user goes back into the textbox, you want to remove the formatting. You can do this in the TextBox's GotFocus event. You can use String.Replace to manually remove $ and , characters, leaving you with a clean string you can convert to Int and use to update TextBox.Text.

    You should also create a Function for the code you use to convert a number to currency, that will save you from copying it in multiple locations in your code:

    Public Function GetCurrencyString(currency As Single) As String
        Dim culture As CultureInfo = CultureInfo.GetCultureInfo("en-US")
        Dim numberFormat As NumberFormatInfo = CType(culture.NumberFormat.Clone(), NumberFormatInfo)
        numberFormat.CurrencySymbol = "$ "
        Return String.Format(numberFormat, "{0:C}", currency)
    End Function