Only Numeric Values in the textbox

G

Guest

I have written the following code which allows user to enter only numbers and
only one decimal point. This works fine.

However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User should
only be allowed to enter two digits after decimal point.

I know I can validate it in the validate event, however, I was wondering if
some can modify the following code so that it be done in keypress event.

Thanks in advance!


If Asc(e.KeyChar) <> Keys.Back Then
If e.KeyChar.IsNumber(e.KeyChar) = False Then
If e.KeyChar <> "." Then
ze.Handled = True
Else
If txtAmountFigure.Text.IndexOf(".") <> -1 Then
e.Handled = True
End If
End If
End If
End If
 
Q

Qwert

REM txtAmountFigure = control.
Dim intLoc As Integer = txtAmountFigure.Text.LastIndexOf("."c)
e.Handled = true
If intLoc >= 0 Then
If .Text.Substring(intLoc).Length <= 2 Then
e.Handled = False
End If
Else
e.Handled = False
End If
 
H

Herfried K. Wagner [MVP]

patang said:
I have written the following code which allows user to enter only numbers
and
only one decimal point. This works fine.

However, I want that user shouldn't enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User
should
only be allowed to enter two digits after decimal point.

I know I can validate it in the validate event, however, I was wondering
if
some can modify the following code so that it be done in keypress event.


I don't think that this is a good idea. Validation should be made when the
control is validated ('Validating' event), not when the user enters text.
This allows the user to do in-place editing of content pasted from the
clipboard, for example. Typically an error provider is set if the text
entered into the textbox is invalid.
 
C

Crouchie1998

Put this into the KeyPress event:

Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case 48 To 57, 8, 13
Case 45
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
If Me.SelectionStart <> 0 Then
KeyAscii = 0
End If
Case 46
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If

This accepts a decimal point, a minus sign too.

If Herfried wants to be pedantic about the above code then I will post the
code to stop all copy/paste/cut shorcut handlers too because Herfried once
before mentioned it. Although, he never produced a solution.
 
G

Guest

Your code is not doing what I want.

I don't want user to enter more than 2 digits after the decimal place.
 
G

Guest

Your code doesn't work.

Because once user enters two digits AFTER the decimal place he CANNOT enter
anything even on the LEFT HAND SIDE of the decimal place. That's not good.
 
G

Guest

Don't you think it would be better to PREVENT user TYPING alphabets in the
numeric-only field instead of letting him enter first then validating it
later on and prompting a message.
 
H

Herfried K. Wagner [MVP]

Crouchie1998 said:
If Herfried wants to be pedantic about the above code then I will post the
code to stop all copy/paste/cut shorcut handlers too because Herfried once
before mentioned it. Although, he never produced a solution.

Do you think it's more user-friendly to disallow the user to use the
clipboard?!
 
H

Herfried K. Wagner [MVP]

patang said:
Don't you think it would be better to PREVENT user TYPING alphabets in the
numeric-only field instead of letting him enter first then validating it
later on and prompting a message.

I typically allow the user to enter numbers in hexadecimal format (at least
when the textbox accepts integer numbers). This allows the user to paste
output of another program from the clipboard without the need to manipulate
it. In addition to that, I allow the user to enter thousands separators
("1,000,000.000,000"). 'Integer.Parse'/'Single.Parse'/'Double.Parse' have
an overloaded version which allows to specify different formats.
 
Q

Qwert

Ok...how about this one, a TextChanged event:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged

Dim intLoc As Integer = Me.TextBox1.Text.LastIndexOf("."c)
Dim intPos As Integer = Me.TextBox1.SelectionStart()

If intLoc >= 0 Then
If Me.TextBox1.Text.Substring(intLoc).Length > 3 Then
Me.TextBox1.Text = Me.TextBox1.Text.Substring(0,
Me.TextBox1.Text.Length - 1)
Me.TextBox1.SelectionStart = intPos
End If
End If

End Sub
 
Q

Qwert

Sorry this is better:

Dim intPos As Integer = Me.TextBox1.SelectionStart()
Dim intLoc As Integer = .Text.LastIndexOf("."c)
If intLoc >= 0 Then
If .Text.Substring(intLoc).Length <= 2 Or intPos <= intLoc Then
e.Handled = False
End If
Else
e.Handled = False
End If
 
R

Ray Cassick \(Home\)

Might I suggest that if you really want to do this as the user types you
abandon the attempt at blocking key strokes and do the following:

1) grab the string as the person types
2) run the string through a regex parser to see if it matches the pattern
3) if it matches the pattern allow the change. If it does not match the
pattern revert back to the last known good string.

I have not tested this out but it sounds like the start of a sound real time
validation scheme to me.
 
R

Ray Cassick \(Home\)

RegEx = Regular expressions.

RegEx rules are a mthod of expressing the format of a string. You can build
a rule that expresses how you want your string to look and then test a
string against that rule to see if it matches.

For example, here is some simple code to test if a string is a properly
formatted GUID:

Dim guidRegex As New
Regex("[a-fA-F0-9]{8}[-][a-fA-F0-9]{4}[-][a-fA-F0-9]{4}[-][a-fA-F0-9]{4}[-][a-fA-F0-9]{12}")

If (Not guidRegex.IsMatch(value)) Then
Throw New StringNotValidGuidFormatException

End If

This code will allow a tring that looks like
"4A6A5042-6EF4-49a9-8422-C622B75525B2" to match but one that looks like
"4A6A5042:6EF4:49a9:8422:C622B75525B2" will not match.

Run a search on google for regex and you will find all types of examples on
using this powerful tool.
 
H

Hal Rosser

Once you get the bugs worked out, you might want to create a new control.
You could call it a "NumberBox" control.
Your idea is doable and usable - (and reusable).
 
J

james

Been watching this thread and find it interesting. Here is a simple thing I put together using some of Qwert's code:
Put two textboxes on a form add this to Textbox1's KeyUp event:

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp

Dim a As String

If TextBox1.Text Like "*[!. 0-9]*" Then

TextBox2.Text = ""

Exit Sub

ElseIf TextBox1.Text = "" Then

Exit Sub

Else

End If

Dim intLoc As Integer = Me.TextBox1.Text.LastIndexOf("."c)

If intLoc >= 0 Then

If Me.TextBox1.Text.Substring(intLoc).Length = 3 Then' had to change this to 3 to get two places to the right of the decimal

e.Handled = True

a = TextBox1.Text

TextBox2.Text = a

End If

Else

e.Handled = False

Exit Sub

End If

End Sub

Not quite perfect as it will allow entry of more than two characters to the right of the decimal in Textbox1. But, the extra
characters will not show up in Textbox2. So, maybe, there is an idea here somewhere.
Also, even if you enter Characters other than 0-9 and the Decimal in Textbox1, they will not show up in Textbox2.
I also tried this in the KeyDown and the KeyPress events too. Almost the same results.
Kind of fun to play with. Maybe, you can make something out of it.
james
 

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