Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Format a textbox for currency input (VB.NET)

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
9 Jan 2012CPOL 80.9K   7   3
Format a textbox for currency input (VB.NET)
I have used code from these two links:
  1. MSDN site[^]
  2. Automatically put decimal for currency format in TextBox when typing[^]

to code a textbox (unbound) for accepting numeric input with 2 decimal places.
The cursor is placed on the right and the text flows from right to left.
The text-align property of the texbox has been set to Right.

VB
Dim strCurrency As String = ""
Dim acceptableKey As Boolean = False

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) OrElse (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse e.KeyCode = Keys.Back Then
            acceptableKey = True
        Else
            acceptableKey = False
        End If
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        ' Check for the flag being set in the KeyDown event.
        If acceptableKey = False Then
            ' Stop the character from being entered into the control since it is non-numerical.
            e.Handled = True
            Return
        Else
            If e.KeyChar = Convert.ToChar(Keys.Back) Then
                If strCurrency.Length > 0 Then
                    strCurrency = strCurrency.Substring(0, strCurrency.Length - 1)
                End If
            Else
                strCurrency = strCurrency & e.KeyChar
            End If

            If strCurrency.Length = 0 Then
                TextBox1.Text = ""
            ElseIf strCurrency.Length = 1 Then
                TextBox1.Text = "0.0" & strCurrency
            ElseIf strCurrency.Length = 2 Then
                TextBox1.Text = "0." & strCurrency
            ElseIf strCurrency.Length > 2 Then
                TextBox1.Text = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
            End If
            TextBox1.Select(TextBox1.Text.Length, 0)
          
        End If
e.Handled = True
    End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionModified Version Pin
SumitSaha17-Jun-16 17:04
SumitSaha17-Jun-16 17:04 
GeneralReason for my vote of 3 Very surprising to me ,a beginner~~~ Pin
tdyso_zmh10-Jan-12 23:52
tdyso_zmh10-Jan-12 23:52 
GeneralWhy not use a numeric updown control? Your solution, whilst ... Pin
John Brett10-Jan-12 2:56
John Brett10-Jan-12 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.