-
Sep 16th, 2007, 11:18 AM
#1
Thread Starter
Addicted Member
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.
-
Sep 16th, 2007, 01:40 PM
#2
PowerPoster
Re: MaskedTextBox Currency Mask?
you might want to set the RightToLeft property of the MaskedTextBox to Yes
-
Sep 16th, 2007, 08:17 PM
#3
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:
Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim currency As Decimal
'Convert the current value to currency, with or without a currency symbol.
If Not Decimal.TryParse(Me.TextBox1.Text, _
Globalization.NumberStyles.Currency, _
Nothing, _
currency) Then
'Don't let the user leave the field if the value is invalid.
With Me.TextBox1
.HideSelection = False
.SelectAll()
MessageBox.Show("Please enter a valid currency amount.", _
"Invalid Value", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
.HideSelection = True
End With
e.Cancel = True
End If
End Sub
Private Sub TextBox1_Validated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.Validated
'Display the value as local currency.
Me.TextBox1.Text = Decimal.Parse(Me.TextBox1.Text).ToString("c")
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.
-
Feb 11th, 2012, 11:24 AM
#4
New Member
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("$", "") .
-
Feb 11th, 2012, 10:54 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|