Replace char in the OnKeyPress

  • Thread starter Thread starter Marc Robitaille
  • Start date Start date
M

Marc Robitaille

Hello,

I think it is a easy question but I don't have any clue how to do it...Is it
possible to replace the char that a user type in a textbox with an other one
in the OnKeyPress methode? My textbox support 4 numbers types:
1-Integer
2-Decimal
3-Currency
4-Percent

I made a property with an Enum to support that. When the property NumberType
is set to 2, 3 or 4, I want the textbox to make no difference between the
"," and the "."

If the regional setting is set to "," and the user type "." I want my
textbox to change to the "," char. I validate this in the OnKeyPress methode
but I endup with the "." in the Textbox. I do something like this in the
OnKeyPress methode when NumberType is set to 2.

e = New
KeyPressEventArgs(convert.tochar(system.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
))

I know that this is not the right solution because it's not working.

I need help

Thank you
 
Marc,

You mean something quick and dirty made as this?

For others, this can be (sometimes) a problem in countries where a US
keyboard fulfils all the needs except the decimal comma

\\\
Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Decimal OrElse e.KeyCode = Keys.OemPeriod Then
Dim pos As Integer = TextBox1.Text.Length - 1
TextBox1.Text = TextBox1.Text.Substring(0, pos) & Chr(44)
TextBox1.SelectionStart = TextBox1.Text.Length
TextBox1.SelectionLength = 1
End If
End Sub
///

I have beside the num keyboard as well taken the period, in my opinion it is
better when you keep that period on the normal keyboard the same and delete
that part from this code.

I hope this helps?

Cor
 
Hey! Thank you

It is exactly what I wanted. Simple and effective
Thank you again
 
Back
Top