How do i verify that an integer value has been entered into a textbox?

  • Thread starter Thread starter YardDancer
  • Start date Start date
Y

YardDancer

Hi all

I don't want to use the the Cint() or CDec() or other VB.Net Functions and
I dont want to restrict keystrokes in the textbox.

I am also using the "strict type semantic" settings that requires you to
explicitly state all conversions.
 
YardDancer said:
I don't want to use the the Cint() or CDec() or other VB.Net Functions
and I dont want to restrict keystrokes in the textbox.

\\\
Private Sub TextBox1_Validating( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles TextBox1.Validating
Dim SourceControl As TextBox = DirectCast(sender, TextBox)
Dim n As Integer
If Not Integer.TryParse(SourceControl.Text, n) Then
Me.ErrorProvider1.SetError( _
SourceControl, _
"Value must be an integer." _
)
Else
If Me.ErrorProvider1.GetError(SourceControl).Length > 0 Then
Me.ErrorProvider1.SetError(SourceControl, "")
End If
...
End If
End Sub
///
 
I am using VS2003

Yes but i want to be more specific. after I have established that it is a
number then i want to find out what type of number floating point or integer

Convert.ToInt32(textbox.Text) throws Input string was not in a correct
format exception

Integer.Parse(textbox.text) Int32.Parse(textbox.text) both throw Input
string was not in a correct format exception


If Isnumeric (textbox.text) then
If CDec(textbox.text) <> Cint(textbox.text) Then ' this works

Else
.......
End if
Else
....
End if

Remember i do not want to restrict keystrokes in the textbox
 
Do you need to do this check for only one control, or do you need it
for several controls in your application?

I'm a big fan of inheriting a standard control and adding in my own
properties and methods.
In 2003 you create a usercontrol.
Edit the class and modify the Inherits UserControl to Inherits
TextBox. The UI goes away from the form, because you are now adding
functionality to the standard textbox instead of a user control.

Now you can create any properties, methods, and member variables that
you want to use in your application.

Public Function IsInteger() as boolean
your code to check for integer

Public Function IsDecimal() as boolean
your code to check for decimal

I like to use add on functionality that's similar to microsoft syntax.
Microsoft has a .ToString. I
add .ToInteger, .ToDecimal, .ToSingle, ...

Hope it helps.
 
Yard Dancer,

Why don't you build your own CInt or CDec Microsoft Net functions, probably
the latter is a better name than that used by you, because the namespace
where it is in starts with Microsoft and not with VisualBasic.
(Although AFAIK Mono has the same).

Cor
 
Back
Top