Dear Sir ,
I'm Trying to remove extra space and currency string formatting in textbox with VB.NET
Link screenshot example 1
Link screenshot example 2
The code I use may also be wrong and please guide me to use the correct event to do the formatting in the textbox
Please Guide me
Example 1
TOTAL INVOICE : Rp 250.000,00
DOWN PAYMENT :
'Extra Space in txtvalueinwords
Dua Ratus Empat Puluh Ribu Rupiah
'in lblbalance
Rp 240.000,00
Example 2
format becomes lost in txttotalinvoice when i make txtdownpayment empty
TOTAL INVOICE : Rp 250.000,00
DOWN PAYMENT :
Dua Ratus Empat Puluh Sembilan Ribu Sembilan Ratus Sembilan Puluh Sembilan Rupiah
'but in lblbalance not correct become like below
Rp 249.999,00
Imports System.Globalization
Public Class Form5
Public Function Terbilang(ByVal nilai As Integer) As String
Dim bilangan As String() = {"", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas"}
If nilai < 12 Then
Return " " & System.Text.RegularExpressions.Regex.Replace(bilangan(CInt(nilai)), "^\s+|\s+$", " ")
ElseIf nilai < 20 Then
Return System.Text.RegularExpressions.Regex.Replace(Terbilang(nilai - 10) & " Belas", "^\s+|\s+$", " ")
ElseIf nilai < 100 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 10))) & " Puluh") + Terbilang(nilai Mod 10), "^\s+|\s+$", " ")
ElseIf nilai < 200 Then
Return System.Text.RegularExpressions.Regex.Replace(" Seratus" & Terbilang(nilai - 100), "^\s+|\s+$", " ")
ElseIf nilai < 1000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 100))) & " Ratus") + Terbilang(nilai Mod 100), "^\s+|\s+$", " ")
ElseIf nilai < 2000 Then
Return System.Text.RegularExpressions.Regex.Replace(" Seribu" & Terbilang(nilai - 1000), "^\s+|\s+$", " ")
ElseIf nilai < 1000000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 1000))) & " Ribu") + Terbilang(nilai Mod 1000), "^\s+|\s+$", " ")
ElseIf nilai < 1000000000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 1000000))) & " Juta") + Terbilang(nilai Mod 1000000), "^\s+|\s+$", " ")
ElseIf nilai < 1000000000000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 1000000000))) & " Milyar") + Terbilang(nilai Mod 1000000000), "^\s+|\s+$", " ")
ElseIf nilai < 1000000000000000 Then
Return System.Text.RegularExpressions.Regex.Replace((Terbilang(CInt((nilai \ 1000000000000))) & " Trilyun") + Terbilang(CInt(nilai Mod 1000000000000)), "^\s+|\s+$", " ")
Else
Return ""
End If
End Function
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Valuetotalinvoice As Integer = CInt(250000)
Dim Valuedownpayment As Integer = 0
Integer.TryParse(txttotalinvoice.Text, Valuetotalinvoice)
Integer.TryParse(txtdownpayment.Text, Valuedownpayment)
Dim culture As CultureInfo = CultureInfo.GetCultureInfo("id-ID")
Dim numberFormat As NumberFormatInfo = CType(culture.NumberFormat.Clone(), NumberFormatInfo)
numberFormat.CurrencySymbol = "Rp "
txttotalinvoice.Text = String.Format(numberFormat, "{0:C}", CInt(250000))
txttotalinvoice.ReadOnly = True
txtvalueinwords.ReadOnly = True
Dim UserInput As Integer
If Integer.TryParse(txtdownpayment.Text, UserInput) Then
txtvalueinwords.Text = Terbilang(CInt(Valuetotalinvoice) - CInt(Valuedownpayment)) + " Rupiah"
End If
End Sub
Private Sub txtdownpayment_KeyUp(sender As Object, e As KeyEventArgs) Handles txtdownpayment.KeyUp
txttotalinvoice.Text = txttotalinvoice.Text.Replace("Rp ", "").Replace(",00", "").Replace(".", "")
If Not String.IsNullOrEmpty(txtdownpayment.Text) Then
Dim Valuedownpayment As Integer = 0
Dim Valuetotalinvoice As Integer = 0
Integer.TryParse(txtdownpayment.Text, Valuedownpayment)
Integer.TryParse(txttotalinvoice.Text, Valuetotalinvoice)
Dim UserInput As Integer
Dim culture As CultureInfo = CultureInfo.GetCultureInfo("id-ID")
Dim numberFormat As NumberFormatInfo = CType(culture.NumberFormat.Clone(), NumberFormatInfo)
numberFormat.CurrencySymbol = "Rp "
lblbalance.Text = String.Format(numberFormat, "{0:C}", Convert.ToInt32(Valuetotalinvoice - Valuedownpayment))
txttotalinvoice.Text = String.Format(numberFormat, "{0:C}", Valuetotalinvoice)
If Integer.TryParse(txtdownpayment.Text, UserInput) Then
txtvalueinwords.Text = Terbilang(CInt(Valuetotalinvoice) - CInt(Valuedownpayment)) + " Rupiah"
End If
End If
End Sub
Private Sub textbox(sender As Object, e As EventArgs) Handles txtdownpayment.TextChanged
txttotalinvoice.Text = txttotalinvoice.Text.Replace("Rp ", "").Replace(",00", "").Replace(".", "")
If Not String.IsNullOrEmpty(txtdownpayment.Text) Then
Dim Valuedownpayment As Integer = 0
Dim Valuetotalinvoice As Integer = 0
Integer.TryParse(txtdownpayment.Text, Valuedownpayment)
Integer.TryParse(txttotalinvoice.Text, Valuetotalinvoice)
Dim UserInput As Integer
Dim culture As CultureInfo = CultureInfo.GetCultureInfo("id-ID")
Dim numberFormat As NumberFormatInfo = CType(culture.NumberFormat.Clone(), NumberFormatInfo)
numberFormat.CurrencySymbol = "Rp "
lblbalance.Text = String.Format(numberFormat, "{0:C}", Convert.ToInt32(Valuetotalinvoice - Valuedownpayment))
txttotalinvoice.Text = String.Format(numberFormat, "{0:C}", Valuetotalinvoice)
If Integer.TryParse(txtdownpayment.Text, UserInput) Then
txtvalueinwords.Text = Terbilang(CInt(Valuetotalinvoice) - CInt(Valuedownpayment)) + " Rupiah"
End If
End If
End Sub
End Class