only integer

  • Thread starter Thread starter Maarten
  • Start date Start date
Larry,

Larry Serflaten said:
Well, I know that, but what are the alternatives in this particular case,
as
long as there is no 'Int32.TryParse'? OK, maybe we could utilize
'Double.TryParse' or regular expressions + range check, but this is IMO
far
too complicated to implement and should be possible out of the box (which
is
the case for .NET 2.0).

Too complicated? Since when has writting user interface code been a
breeze ???
<g>
[...]
A more robust solution would be:

Dim user As String
Dim value As Integer
Do
user = InputBox("Enter a number")
If user.Length = 0 OrElse user Like "*[!0-9]*" Then
MsgBox("A numeric value is required to continue.")
Else
If user.Length < 11 _
AndAlso CLng(user) <= Integer.MaxValue _
AndAlso CLng(user) >= Integer.MinValue Then
value = CInt(user)
Exit Do
Else
MsgBox("Please enter a whole number between " _
& Integer.MinValue & " and " &
Integer.MaxValue)
End If
End If
Loop

'CLng' can throw an exception too when the parsed number does not fit into
the 'Long' datatype...

;-)
 
the Op want's a number, see what i want do do is an option to set the
timerspeed of a timer

this can be a number from 0 to 1,2,3,4,5,....10 000 and more
but if the text in the inputbox is like "jjfjfj" or "1.555" or "^^4654klj"
, i need a messagebox that says please enter a valid number to set the
timerspeed.

kind regards Maarten

Ps: thanks for all the reply's i realy appreciate it
 
Hey OP, 1.555 is a number. :^) You want a whole number.

I stand by my original suggestion of using regex expression:

Dim s as string = InputBox("Value", "Read frequenty<sic>")

Dim rNumber As New System.Text.RegularExpressions.Regex("^\d*$")
If rNumber.IsMatch(s) Then
msgbox("ok")
Else
msgbox("not a whole number")
End If

Greg
 
'CLng' can throw an exception too when the parsed number does not fit into
the 'Long' datatype...

It would be impossible to overflow a Long type with only 10 digits....

<g>
LFS
 
Greg Burns said:
I stand by my original suggestion of using regex expression:

Dim s as string = InputBox("Value", "Read frequenty<sic>")

Dim rNumber As New System.Text.RegularExpressions.Regex("^\d*$")
If rNumber.IsMatch(s) Then
msgbox("ok")
Else
msgbox("not a whole number")
End If

That certainly seems like an easy solution to me...

Now if I could only bring myself to learn more about that RegEx syntax!

;-)
LFS
 
Maarten,

First, I don't understand why I see such a long thread while everything in
my opinion is alreayd told in the the thread I showed you about this (last
message in this thread).

However, when you with a valid number would be in my opinion "dim myinteger
as integer = Cint(textbox.text)" and as well a limit of the max and the
minimum of the integer all you have to do, or my example I show you in the
thread where the maximum positions of the textbox are limited.

However there is more written in that thread as well by Larry, Herfried, and
Alex.

Cor
 
Larry,

Larry Serflaten said:
It would be impossible to overflow a Long type with only 10 digits....

<g>

Oh, I missed that... :-(((.
 
Larry Serflaten said:
It would be impossible to overflow a Long type with only 10 digits....

<g>
LFS

The user could enter "10E200" AFAIS...
 
Maarten said:
hi all,

how can i retrieve the type if a value inserted in an inputbox.

what i want to do is make shure people only insert an integer type, if they
don't they get an error message.

The way I do it is to add a handler to the keypress event and filter out
any character not in the 0-9 range. That way they can't even enter non
numeric values. I don't understand why noone in this thread has thought
about that option yet.
 
C-Services Holland b.v. said:
The way I do it is to add a handler to the keypress event and filter out
any character not in the 0-9 range. That way they can't even enter non
numeric values. I don't understand why noone in this thread has thought
about that option yet.


Re-read the first sentence, you don't get events from the InputBox....

;-)
LFS
 
thank you verry much, this works fine and is just what i needed.

regards,
Maarten
 
Hi all

thanks for the plenty of replies, it works fine now

i used the code of greg and does a great job

Dim s as string = InputBox("Value", "Read frequenty<sic>")

Dim rNumber As New System.Text.RegularExpressions.Regex("^\d*$")
If rNumber.IsMatch(s) Then
msgbox("ok")
Else
msgbox("not a whole number")
End If


kind regards,
Maarten
 
Maarten said:
i used the code of greg and does a great job

Dim s as string = InputBox("Value", "Read frequenty<sic>")

Dim rNumber As New System.Text.RegularExpressions.Regex("^\d*$")
If rNumber.IsMatch(s) Then
msgbox("ok")
Else
msgbox("not a whole number")
End If

Mhm... The code won't prevent the user from entering a number that does not
fit into an 'Integer' or 'Long' -- and thus will throw an exception when
parsing.
 
thanks,

i can't figure that out, becouse the source is set to a textbox type, but
what textbox?

i tried to set the source to the variable s of the inputbox, but can't get
it to work

i'm intersted in this tread becouse it uses the errorprovider, and i like to
know more about is

i beleave it is new in vb.net or am i wrong?.

Thanks,
Maarten
 
Larry said:
Re-read the first sentence, you don't get events from the InputBox....

;-)
LFS

GAH inputbox... that's why I made my own input class :) Basically looks
the same as the inputbox, but with all the events exposed.
 
Just one more thing. ;)
First, I don't understand why I see such a long thread while everything in
my opinion is alreayd told in the the thread I showed you about this (last
message in this thread).

No mention of a regex solution in the aforementioned thread...
However, when you with a valid number would be in my opinion "dim
myinteger as integer = Cint(textbox.text)"

Yeah, if you have a valid number this will work, otherwise you get an
exception. It gives no feedback if the user enters a decimal number. Isn't
that the whole point of this thread: validating a whole number?
...as well a limit of the max and the minimum of the integer all you have
to do, or my example I show you in the thread where the maximum positions
of the textbox are limited.

The OP is using an InputBox, not a TextBox control.

Greg
 
Greg,

I missed the inputbox, he answered it to me as well, however thanks.
(This can be done than of course with .length)
However when the regex function fits, than why botter it is nice short.

:-)

Cor
 

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

Back
Top