Results 1 to 5 of 5

Thread: MaskedTextBox Currency Mask?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Location
    Miami, FL USA
    Posts
    171

    Lightbulb MaskedTextBox Currency Mask?

    Does anyone know how to set the maskedtextbox to a currency format that actually works? I have set the mask to “$999,999.00” but the user input looks like this “$120,0__.__” instead of this “$___._12.00”. It seems like the maskedtextbox would be able to handle this type of input without using the keypress events to alter the text.

  2. #2

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: MaskedTextBox Currency Mask?

    The MaskedTextBox is not a panacea because it requires a fixed length mask. I suggest not using a MaskedTextBox at all. Just use a NumbericUpDown with the DecimalPlaces property set to 2 or a regular TextBox and validate it for currency:
    vb.net Code:
    1. Private Sub TextBox1_Validating(ByVal sender As Object, _
    2.                                 ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    3.     Dim currency As Decimal
    4.  
    5.     'Convert the current value to currency, with or without a currency symbol.
    6.     If Not Decimal.TryParse(Me.TextBox1.Text, _
    7.                             Globalization.NumberStyles.Currency, _
    8.                             Nothing, _
    9.                             currency) Then
    10.         'Don't let the user leave the field if the value is invalid.
    11.         With Me.TextBox1
    12.             .HideSelection = False
    13.             .SelectAll()
    14.  
    15.             MessageBox.Show("Please enter a valid currency amount.", _
    16.                             "Invalid Value", _
    17.                             MessageBoxButtons.OK, _
    18.                             MessageBoxIcon.Error)
    19.  
    20.             .HideSelection = True
    21.         End With
    22.  
    23.         e.Cancel = True
    24.     End If
    25. End Sub
    26.  
    27. Private Sub TextBox1_Validated(ByVal sender As Object, _
    28.                                ByVal e As System.EventArgs) Handles TextBox1.Validated
    29.     'Display the value as local currency.
    30.     Me.TextBox1.Text = Decimal.Parse(Me.TextBox1.Text).ToString("c")
    31. End Sub
    This requires the user to enter a valid number before leaving the field, with or without a currency symbol. When the user does leave the field the value will always be displayed in currency format.

  4. #4
    New Member
    Join Date
    Feb 2012
    Posts
    1

    Re: MaskedTextBox Currency Mask?

    I found the above code by jmcilhinney to be very helpful. I did encounter one problem. (I am coding in VB.NET using Visual Basic 2010 Express.) If the user manually entered a "$" into the textbox there an unhandled exception would result. I eventually fixed this by adding one line of code into the Sub Textbox1_Validating(...). The line I added comes after the first line in the Sub Textbox1_Validating Dim currency As Decimal. I added the line
    Textbox1.text = Textbox1.text.replace("$", "") .

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: MaskedTextBox Currency Mask?

    If you follow the CodeBank link in my signature you'll find a link to my Numeric Text Box thread, which provides a custom TextBox control that was written much more recently than the post here to handle just about any numeric format, including currency. It will allow the user to include or omit the currency symbol in the data, reformatting the text correctly when the control loses focus regardless.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width