how to check for numeric and enter in text box

  • Thread starter Thread starter anthonymelillo
  • Start date Start date
A

anthonymelillo

Is there any way to check the entry in a text box and make sure it is
numeric ?

Also, can I catch the enter key and make it call a button click event ?

Sorry if this sounds stupid, but I am trying to come to grips with the
differences in .Net since all my prior programming was done in VB6

Thanks
 
anthonymelillo said:
Is there any way to check the entry in a text box and make sure it
is numeric ?

Also, can I catch the enter key and make it call a button click event
?

Have a look at the Defaultbutton property of the Form.
Sorry if this sounds stupid, but I am trying to come to grips with
the differences in .Net since all my prior programming was done in
VB6

I prefer not to limit the user's input when typing. Try to convert the
Textbox' content to a numeric value using the (shared) Parse function of the
destination type. Catch occuring exceptions to find out if conversion was
successful.
 
anthonymelillo said:
Is there any way to check the entry in a text box and make sure it is
numeric ?

You can use 'Double.TryParse' or 'IsNumeric'.
Also, can I catch the enter key and make it call a button click event ?

Create a button and assign it to the form's 'AcceptButton' property.
 
try the IdNumeric() function....
if IsNumeric then
do something cool
else
messageBox("So non-cool error")
end if

hope it helps
 
Stephen said:
try the IdNumeric() function....
if IsNumeric then
do something cool
else
messageBox("So non-cool error")
end if

I think, IsNumeric does not accept exactly the same formats as the Parse
method of the types does. It also can not check if it is within the value
range of the destination data type.
 
Armin,
Seriously I d'not understand this answer, (I hope you are not talking about
dragged information to the textbox).
I think, IsNumeric does not accept exactly the same formats as the Parse
method of the types does. It also can not check if it is within the value
range of the destination data type.

In a textbox is normaly only text, so what is wrong to ask in a button event
"if not isnumeric(textbox1.text)".
I am really curious (for a simple method, not the best maybe)?

And for the not simple method I am as well as intrested?
:-) just to say that is well ment.
Cor
 
Cor said:
Armin,
Seriously I d'not understand this answer, (I hope you are not talking
about dragged information to the textbox).


In a textbox is normaly only text, so what is wrong to ask in a
button event "if not isnumeric(textbox1.text)".
I am really curious (for a simple method, not the best maybe)?

And for the not simple method I am as well as intrested?
:-) just to say that is well ment.
Cor

Dim i As Integer
Dim s As String = "1234567890.12"
If IsNumeric(s) Then
i = Integer.Parse(s)
End If

Means: Calling IsNumeric doesn't help to find out whether the string can be
converted. The only way to find it out is trying to convert it (or call
Double.TryParse as Herfried mentioned).
 
Cor said:
Armin,
Seriously I d'not understand this answer, (I hope you are not talking
about dragged information to the textbox).


In a textbox is normaly only text, so what is wrong to ask in a
button event "if not isnumeric(textbox1.text)".
I am really curious (for a simple method, not the best maybe)?

And for the not simple method I am as well as intrested?
:-) just to say that is well ment.
Cor


....and what I was trying to say in my first post: ;-)
Usually you also need to convert the string to a numeric data type (not only
check if it is numeric). I am not 100% sure, but I think that IsNumeric does
not perform exactly the same check as the conversion function called later.
Therfore IsNumeric does not help here.
 
Armin,
I try it and what the culture does with it, I give you an anser then.
Take some time (I have no time for that, but when it is longer then 3 days
it is never :-)
But I am intrested so I think it will be tomorrow.

Thanks,
Cor
 
Armin Zingler said:
I think, IsNumeric does not accept exactly the same formats as the Parse
method of the types does. It also can not check if it is within the value
range of the destination data type.

This depends on the data type of the destination variable.
 
Herfried K. Wagner said:
This depends on the data type of the destination variable.

Yes, that's why IsNumeric can not check it. (I thought I mentioned... ;-)
 
Armin,
As promissed,
This was a result from a little test.
\\\
'Western Europe Culture Settings
Dim s As String = "9,12"
If IsNumeric(s) Then
Dim i As Integer = CInt(s) '= 9
Dim j As Decimal = CDec(s) '= 9.12D
Dim h As Byte = CByte(s) '=9
Dim k As Integer = Integer.Parse(s) '=Error
End If
///
Or do I see something wrong?
Cor
 
Cor said:
Armin,
As promissed,
This was a result from a little test.
\\\
'Western Europe Culture Settings
Dim s As String = "9,12"
If IsNumeric(s) Then
Dim i As Integer = CInt(s) '= 9
Dim j As Decimal = CDec(s) '= 9.12D
Dim h As Byte = CByte(s) '=9
Dim k As Integer = Integer.Parse(s) '=Error
End If
///
Or do I see something wrong?
Cor

I don't know *what* you are seeing!?? :-) Do you agree with my statement or
don't you? I'd say you do! ;-) The example shows that the success of
IsNumeric is no guarantee for the success of the conversion. That's why I
wouldn't use IsNumeric. Instead I try to convert the value and catch
occuring
errors.
 
Hi Armin,
I don't know *what* you are seeing!?? :-) Do you agree with my statement or
don't you? I'd say you do! ;-) The example shows that the success of
IsNumeric is no guarantee for the success of the conversion. That's why I
wouldn't use IsNumeric. Instead I try to convert the value and catch
occuring
errors.

Now I see it, you expanded the question,

That I did not.

And I think there is nothing wrong with dim mydec as decimal =
cdec(textbox.text) in this situation, it describes exact what you want to
do.

Cor
 
Armin Zingler said:
I don't know *what* you are seeing!?? :-) Do you agree with my statement or
don't you? I'd say you do! ;-) The example shows that the success of
IsNumeric is no guarantee for the success of the conversion. That's why I
wouldn't use IsNumeric. Instead I try to convert the value and catch
occuring

Nevertheless 'IsNumeric' returns a correct value.
 
Armin Zingler said:
Depends on how you define "correct". ;-)

'IsNumeric' returns 'True' if the value passed to it is numeric. Even a
string which doesn't fit into a byte can be numeric.

;-)
 
I finally found a cool solution to my question and it works great. Just
wanted to post it and let you all take a look:

Insert this code into the text box keypress event:
Dim keyinteger As Integer
keyinteger = Asc(e.KeyChar)
'48-57 is numbers 0-9
'8 is backspace
'13 is enter
Select Case keyinteger
Case 13
e.Handled = True
Case 8
'backspace-do nothing
Case 48 To 57
'ok this is numeric
Case Else
MsgBox("Entry must be numeric")
e.Handled = True
End Select
 
anthonymelillo said:
I finally found a cool solution to my question and it works great.
Just wanted to post it and let you all take a look:

Insert this code into the text box keypress event:
Dim keyinteger As Integer
keyinteger = Asc(e.KeyChar)
'48-57 is numbers 0-9
'8 is backspace
'13 is enter
Select Case keyinteger
Case 13
e.Handled = True
Case 8
'backspace-do nothing
Case 48 To 57
'ok this is numeric
Case Else
MsgBox("Entry must be numeric")
e.Handled = True
End Select

I consider it to be the worst suggestion (IMHO). What if the user pastes
"invalid" text from the clipboard? Apart from that, I wouldn't limit the
user's input during the edit process. I'd check the value not before leaving
the textobox or before the numeric value is about to be processed (again,
IMO).
 
Back
Top