473,396 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Numeric TextBox Masking..

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
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.
Nov 21 '05 #1
3 6650
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
"Aaron Smith" <th**********@smithcentral.net> wrote in message
news:gJ*****************@newssvr16.news.prodigy.co m...
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
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.

Nov 21 '05 #2
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
"Aaron Smith" <th**********@smithcentral.net> wrote in message
news:gJ*****************@newssvr16.news.prodigy.co m...
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
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.

Nov 21 '05 #3
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...

"Stephen Muecke" <st*****@senet.com.au> wrote in message news:<uN**************@TK2MSFTNGP12.phx.gbl>...
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
"Aaron Smith" <th**********@smithcentral.net> wrote in message
news:gJ*****************@newssvr16.news.prodigy.co m...
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
--
---
Aaron Smith
Remove -1- to E-Mail me. Spam Sucks.

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Matt Feinstein | last post by:
Hi all-- I'm new to Python, and was somewhat taken aback to discover that the core language lacks some basic numerical types (e.g., single-precision float, short integers). I realize that there...
2
by: Sam | last post by:
Why isn't there a numberBox Windows Form control like a textbox control but for numeric (float) input? There doesn't seem to be any easy or visual solution to this VERY SIMPLE ISSUE in either...
3
by: Hai Nguyen | last post by:
Hi All I have a textbox want to mask as phone box. How can I do it? Thanks
16
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
0
by: Aaron Smith | last post by:
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 ...
0
by: Aaron Smith | last post by:
Is there an easy way to do a numeric mask on a DataGridTextBoxColumn? I need to only allow entry of numeric value and have only one decimal. I already have a subclasses TextBox column that limits...
1
by: Luqman | last post by:
My textbox is bound to Typed Dataset, with a Numeric Field, I want the user should not type anything in textbox except Numeric, how can I ? The data type is : Sql Server Decimal(5,2) Currently,...
3
by: mracuraintegra | last post by:
I am seriously at my wits' end here with this. I've got a textbox bound to a money column in a database, but it shows up as a decimal value, meaning that $9.25 shows up as 9.250000. ARRRRG. ...
5
lotus18
by: lotus18 | last post by:
Hello World! I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. Public Enum MyOption Alpha = 1 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.