[VB.NET] Styling Nominal Currency in textbox


Description
     if you make an application for finance but you not styling the text box, it will horrible for user. a mistake cannot be tolerated in finance , Because it is always associated with the company's money. who will be responsible ? definite user of the financial department. IT departments do not want to be responsible as well as the finance department because of lack of applications. to prevent this i will give a function to styling nominal currency like in finance. for example :

2500000 => horrible format , now compare with this 2,500,000 => readable

now this function :


Public Function currencyFormat(ByVal money As String) As String
    Dim formattedString As String = ""
    For i = 1 To money.Length
        If i >= 3 Then
            If i Mod 3 = 1 Then
                formattedString = money.Substring(money.Length - i, 1) & "," & formattedString
            Else
                formattedString = money.Substring(money.Length - i, 1) & formattedString
            End If
        Else
            formattedString = money.Substring(money.Length - i, 1) & formattedString
        End If
    Next i
    Return formattedString
End Function

To Use :
textbox1 => got_focus event :
textbox1 .Text = Replace(textbox1.Text, ",", "")

textbox1 => lostfocus_focus event :
textbox1.Text = currencyFormat(Replace(textbox1.Text, ",", ""))

if you want this value entered in database, don't forget always to sanitize character ',' (comma) to prevent error in query.

work's great :D