Numeric TextBox Masking..

A

Aaron Smith

I found an quick and dirty example of a numeric text box that converts
the string to a currency mask.. Here is the code:

Public Class NumericMaskedTextBox
Inherits System.Windows.Forms.TextBox

Private Sub NumericMaskedTextBox_KeyPress(ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
'the key press is not in the string (of numerals)
If InStr(1, "0123456789.", e.KeyChar, CompareMethod.Text) = 0 Then
If AscW(e.KeyChar) <> Keys.Back Then
'the key press is not enter or delete either
'so say we have handled it
e.Handled = True
End If
Else
If InStr(1, ".", e.KeyChar, CompareMethod.Text) > 0 Then
If InStr(1, Me.Text, ".", CompareMethod.Text) > 0 Then
e.Handled = True
End If
End If
End If
End Sub

Private Sub NumericMaskedTextBox_GotFocus(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.GotFocus
Text = Text.Replace("$", "")
End Sub
End Class

I have a question..

I have this data bound to a field. When the form loads, I would like it
to have the $ in the textbox... Basically, setting the format to
currency.. Is there an event in the textbox I could snag that will do this?

Thanks,
Aaron
 
S

Stephen Muecke

Aaron,

If you are binding, forget the inherited control
Use the standard TextBox and add handlers for the Bindings Format and Parse
events
(assumes the data type is decimal)

Dim myBinding = New Binding("Text", myDataSource, "myDataMember")
With controlBinding
AddHandler .Format, AddressOf DecimalToCurrencyString
AddHandler .Parse, AddressOf CurrencyStringToDecimal
End With
myTextBox.DataBindings.Add(myBinding)

Private Sub CurrencyStringToDecimal(ByVal sender As Object, ByVal cevent As
ConvertEventArgs)
If Not cevent.DesiredType Is GetType(Decimal) Then
Exit Sub
End If
cevent.Value = Decimal.Parse(cevent.Value.ToString,
NumberStyles.Currency)
End Sub

Private Sub DecimalToCurrencyString(ByVal sender As Object, ByVal cevent As
ConvertEventArgs)
If Not cevent.DesiredType Is GetType(String) Then
Exit Sub
End If
cevent.Value = CType(cevent.Value, Decimal).ToString("c")
End Sub
 
A

Aaron

This works, however it is not quite what the user wants. The display
part is only half of it. We need to keep people from trying to enter
"1.5.43" .. While it is okay that when you leave it doesn't error, it
just goes back to the previous value, this is unacceptable to the
people that want the software. When they enter data, they want it to
stop their users from entering a value such as 1.5.45 when then meant
5.45, or even 1.45 ... What I need is field masking.. I have that now
with what I have written and modified further after posting that first
message.. The only thing I need is to display the value like it should
be when the form first loads. I've seen other mask controls that put
$____.__ for the mask, but we personally do not like that. I have been
building on the code I entered earlier, and maybe I will just continue
with that, and try the binding handlers for format and parse in
addition to that...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top