Home All Groups Group Topic Archive Search About

Only Numeric Values in the textbox

Author
27 Mar 2005 6:35 PM
patang
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

Author
27 Mar 2005 7:22 PM
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





Show quoteHide quote
"patang" <pat***@discussions.microsoft.com> schreef in bericht
news:5C12405A-BF26-4670-9C12-7056148BAF3A@microsoft.com...
>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
>
Author
27 Mar 2005 8:19 PM
patang
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.


Show quoteHide quote
"Qwert" wrote:

> 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
>
>
>
>
>
> "patang" <pat***@discussions.microsoft.com> schreef in bericht
> news:5C12405A-BF26-4670-9C12-7056148BAF3A@microsoft.com...
> >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
> >
>
>
>
Author
27 Mar 2005 9:30 PM
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



Show quoteHide quote
"patang" <pat***@discussions.microsoft.com> schreef in bericht
news:645404A0-141F-4B9E-91ED-8C4148DC0DF1@microsoft.com...
> 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.
>
>
> "Qwert" wrote:
>
>> 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
>>
>>
>>
>>
>>
>> "patang" <pat***@discussions.microsoft.com> schreef in bericht
>> news:5C12405A-BF26-4670-9C12-7056148BAF3A@microsoft.com...
>> >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
>> >
>>
>>
>>
Author
27 Mar 2005 9:39 PM
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



Show quoteHide quote
"patang" <pat***@discussions.microsoft.com> schreef in bericht
news:645404A0-141F-4B9E-91ED-8C4148DC0DF1@microsoft.com...
> 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.
>
>
> "Qwert" wrote:
>
>> 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
>>
>>
>>
>>
>>
>> "patang" <pat***@discussions.microsoft.com> schreef in bericht
>> news:5C12405A-BF26-4670-9C12-7056148BAF3A@microsoft.com...
>> >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
>> >
>>
>>
>>
Author
27 Mar 2005 7:45 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"patang" <pat***@discussions.microsoft.com> schrieb:
>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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
27 Mar 2005 7:55 PM
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.
Author
27 Mar 2005 8:15 PM
patang
Your code is not doing what I want.

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


Show quoteHide quote
"Crouchie1998" wrote:

> 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.
>
>
>
Author
27 Mar 2005 8:39 PM
Herfried K. Wagner [MVP]
"Crouchie1998" <crouchie1***@discussions.microsoft.com> schrieb:
> 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?!

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
27 Mar 2005 8:21 PM
patang
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.



Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "patang" <pat***@discussions.microsoft.com> schrieb:
> >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.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
>
Author
27 Mar 2005 8:45 PM
Herfried K. Wagner [MVP]
"patang" <pat***@discussions.microsoft.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
28 Mar 2005 2:26 AM
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






Show quoteHide quote
"patang" <pat***@discussions.microsoft.com> wrote in message news:08D8EE1D-5B13-49D7-A479-6543CB38B392@microsoft.com...
> 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.
>
>
>
> "Herfried K. Wagner [MVP]" wrote:
>
>> "patang" <pat***@discussions.microsoft.com> schrieb:
>> >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.
>>
>> --
>>  M S   Herfried K. Wagner
>> M V P  <URL:http://dotnet.mvps.org/>
>>  V B   <URL:http://classicvb.org/petition/>
>>
>>
Author
27 Mar 2005 8:29 PM
José Joye
You may have a look at this site:
http://www.dotnetmasters.com/samples.htm
http://www.dotnetmasters.com/SampleCode/BillysValidatorsVersion3.zip

Billy Hollis has implemented some nice validators (including numeric one)

José

"patang" <pat***@discussions.microsoft.com> a écrit dans le message de news:
5C12405A-BF26-4670-9C12-7056148BA***@microsoft.com...
Show quoteHide quote
>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
>
Author
27 Mar 2005 9:42 PM
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.

Show quoteHide quote
"patang" <pat***@discussions.microsoft.com> wrote in message
news:5C12405A-BF26-4670-9C12-7056148BAF3A@microsoft.com...
>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
>
Author
27 Mar 2005 9:57 PM
patang
What is "regex parser"?
Author
27 Mar 2005 10:52 PM
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.


Show quoteHide quote
"patang" <pat***@discussions.microsoft.com> wrote in message
news:8EB50E06-B077-48CF-BDE6-F8A09B9C030B@microsoft.com...
> What is "regex parser"?
Author
28 Mar 2005 1:20 AM
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).