Click here to Skip to main content
15,893,588 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 81.1K   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 
Activated Delete key which will delete all the number already pressed
and before pressing decimal key all the key pressed will be whole number
and after pressing the decimal key two number can be pressed for decimal
and no two decimal key could be pressed.
Now the code:
Public Class XTextBox
Inherits System.Windows.Forms.TextBox

Private _Font As Font = New Font("Consolas", 9, FontStyle.Regular)
Public strCurrency As String = ""
Dim acceptableKey As Boolean = False
Private _AddMode As Boolean = False

'http://stackoverflow.com/questions/21016072/boolean-property-will-not-set-to-true
Public Property AddMode() As Boolean
Get
AddMode = _AddMode
End Get
Set(ByVal value As Boolean)
If value <> _AddMode Then
_AddMode = value
'// handle more code changes here.'
'Me.BackColor = Color.FromArgb(255, 255, 0)
strCurrency = ""
End If
'//_AddMode = value
End Set
End Property

Public Shadows Property Font() As Font
Get
Return Me._Font
End Get
Set(ByVal Value As Font)
Me._Font = Value
MyBase.Font = Value
End Set
End Property

Protected Overrides Sub OnCreateControl()
MyBase.Font = Me.Font
'MyBase.TextAlign = Me.TextAlign
MyBase.OnCreateControl()
End Sub

Public Sub New()
Me.TextAlign = HorizontalAlignment.Right
End Sub

'System.ComponentModel.DefaultValue(Windows.Forms.HorizontalAlignment.Right)>
<System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("Indicates how the text should be aligned."), _
System.ComponentModel.Browsable(True)> _
<System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)> _
<System.ComponentModel.Bindable(True)> _
<System.ComponentModel.DefaultValue(GetType(HorizontalAlignment), "Right")> _
Public Shadows Property TextAlign As HorizontalAlignment
Get
Return MyBase.TextAlign
End Get
Set(ByVal Value As HorizontalAlignment)
MyBase.TextAlign = Value
End Set
End Property

'Private Sub XTextBox_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
' If Not IsNumeric(e.KeyChar) And Asc(e.KeyChar) <> 8 And Asc(e.KeyChar) <> 46 Then
' e.Handled = True
' End If
'End Sub

Private Sub XTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Enter
Me.TextAlign = HorizontalAlignment.Right
If Me.ReadOnly = False Then
Me.BackColor = Color.Cyan
End If
End Sub

Private Sub XTextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Leave
If Me.ReadOnly = False Then
Me.BackColor = Color.White
Else
Me.BackColor = SystemColors.Control 'changed on 07/09/2014
End If
'Me.BackColor = Color.White
End Sub

'Private Sub XTextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Validated
' 'Me.Text = Decimal.Parse(Me.Text).ToString("#,#.00#")
' Me.Text = Decimal.Parse(Me.Text).ToString("c")
' 'Me.Text = Decimal.TryParse(Me.Text, _
' 'Globalization.NumberStyles.Currency)

'End Sub

Private Sub XTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.TextChanged
'https://msdn.microsoft.com/en-us/library/xfta99yt(v=vs.90).aspx#
' Function FormatNumber(ByVal Expression As Object, _
' Optional ByVal NumDigitsAfterDecimal As Integer = -1, _
' Optional ByVal IncludeLeadingDigit As TriState = TriState.UseDefault, _
' Optional ByVal UseParensForNegativeNumbers As TriState = TriState.UseDefault, _
' Optional ByVal GroupDigits As TriState = TriState.UseDefault) As String
'From: http://www.vbforums.com/showthread.php?454802-RESOLVED-format-currency-to-Indian-format-in-Label-Textbox
'Me.TextBox1.Text = FormatCurrency("123456789", , , , vbTrue)
'Formatting number in Indian format comma as lakh, thousand and hundred
If Me.Text = String.Empty Then
Me.Text = 0
End If
'If CDbl(Me.Text) > 0 Then
'-----------------------------------------
If IsNumeric(Me.Text) Then
Me.Text = FormatNumber(Me.Text, , , , vbTrue) 'display with 2 decimal & was working properly on 09/06/2016
'Me.Text = FormatNumber(Me.Text, 0, , , vbTrue) 'displaying whole number without decimal
'Me.Text = FormatCurrency(Me.Text, , , , vbTrue) 'Displaying with pound sign
End If

Me.Refresh()
End Sub

Private Sub XTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'strCurrency = ""
'In the Google Search textbox search for "vb.net masked textbox numbers only"
'http://www.codeproject.com/Tips/311959/Format-a-textbox-for-currency-input-VB-NET
'http://stackoverflow.com/questions/11253514/allow-numbers-dots-and-backspace-and-delete-in-textbox?rq=1
'http://stackoverflow.com/questions/613963/how-can-i-allow-only-one-decimal-point-in-a-textbox?rq=1
'http://stackoverflow.com/questions/15410903/textbox-only-allow-numeric-and-one-full-stop-representing-a-decimal-number-v?lq=1

'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 'Original code
'--- The following was added on 11/06/2016 -----------------------------------
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 OrElse e.KeyCode = Keys.Decimal Then
acceptableKey = True
'--- the following was added on 12/06/2016 ---------------------
'--- From http://stackoverflow.com/questions/5589130/check-if-delete-key-is-pressed
ElseIf e.KeyCode = Keys.Delete Then
Me.Text = ""
strCurrency = ""
'MessageBox.Show("Inside Delete Block")
Else
acceptableKey = False
End If
End Sub

Public Overridable Sub XTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
'--- Check for the flag being set in the KeyDown event. -----------------------
'Dim strCurrency As String = "" 'has been declared public in the begining
If acceptableKey = False Then
'Stop the character from being entered into the control since it is non-numerical.
e.Handled = True 'Means to cancel the event
Return
Else
'Determine whether the keystroke is a backspace
If e.KeyChar = Convert.ToChar(Keys.Back) Then
'MessageBox.Show("Inside BackSpace Block")
If strCurrency.Length > 0 Then
strCurrency = strCurrency.Substring(0, strCurrency.Length - 1)
End If
ElseIf e.KeyChar = "." And strCurrency.IndexOf(".") <> -1 Then
' 'http://stackoverflow.com/questions/15410903/textbox-only-allow-numeric-and-one-full-stop-representing-a-decimal-number-v
' 'strCurrency = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
MessageBox.Show("Decimal point has already been pressed")
e.Handled = True 'Means to cancel the event
Return

ElseIf Len(After(strCurrency, ".")) >= 2 Then
'no number to be added after two decimal
e.Handled = True
'MessageBox.Show("Inside decimal more than 2 block" & "strCurrency = " & strCurrency)
'--------------------------------------
Else
strCurrency = strCurrency & e.KeyChar
End If

Me.Text = strCurrency 'Added on 12/06/2016 for number without decimal
'MessageBox.Show(strCurrency)

'-------------------------------------------------------------------

Me.Select(Me.Text.Length, 0)

End If
e.Handled = True
End Sub

Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)
'https://social.msdn.microsoft.com/Forums/en-US/f1f3f153-00b8-4204-ba06-3375b9b4823b/readonly-textbox-default-backcolor?forum=vbgeneral
MyBase.OnEnabledChanged(e)
If Enabled Then
BackColor = Color.White
Else
BackColor = SystemColors.Control
End If
End Sub

Protected Overrides Sub OnReadOnlyChanged(ByVal e As System.EventArgs)
MyBase.OnReadOnlyChanged(e)
If [ReadOnly] Then
BackColor = SystemColors.Control
Else
BackColor = Color.White
End If
End Sub

'You have to set BackColor to the look of a ReadOnly TextBox's BackColor, that is Color.FromKnownColor(KnownColor.Control)
'this is the ReadOnlyChanged event handler for your textbox
'http://stackoverflow.com/questions/18039901/setting-a-read-only-textbox-default-backcolor?rq=1
'Private Sub textBox1_ReadOnlyChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBox1.ReadOnlyChanged
' If textBox1.[ReadOnly] Then
' textBox1.BackColor = Color.FromKnownColor(KnownColor.Control)
' End If
'End Sub
'You may need a variable to store the current BackColor every time your TextBox's BackColor changes:
'Private currentBackColor As Color
'Private suppressBackColorChanged As Boolean
'Private Sub textBox1_BackColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBox1.BackColorChanged
' If suppressBackColorChanged Then
' Return
' End If
' currentBackColor = textBox1.BackColor
'End Sub
'Private Sub textBox1_ReadOnlyChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBox1.ReadOnlyChanged
' suppressBackColorChanged = True
' textBox1.BackColor = If(textBox1.[ReadOnly], Color.FromKnownColor(KnownColor.Control), currentBackColor)
' suppressBackColorChanged = False
'End Sub
'Plz also refer http://stackoverflow.com/questions/15358338/handling-all-textbox-event-in-one-handler
'and https://www.daniweb.com/programming/software-development/threads/454778/readonly-property-in-custom-control

Public Function After(ByVal value As String, ByVal a As String) As String
' Get index of argument and return substring after its position.
Dim posA As Integer = value.LastIndexOf(a)
If posA = -1 Then
Return ""
End If
Dim adjustedPosA As Integer = posA + a.Length
If adjustedPosA >= value.Length Then
Return ""
End If
Return value.Substring(adjustedPosA)
End Function
End Class
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.