tags:

views:

51

answers:

3

I am trying to develop a programe in Visual basic.ne(.NET Framework 3.5) where textbox will only accept integer. User wont be able to type char/decimel or other type.

Can anyone please help me?

Thanks in advance.

A: 

Here's an article describing just the control you need (including not allowing decimals):

Simple Numeric TextBox

The article's code is C#, but as the author notes the control is packaged in a DLL you can use in a VB.NET project.

Jay Riggs
A: 

Just handle the keypress event and test the keychar to make sure it is numeric.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not IsNumeric(e.KeyChar) Then
            e.Handled = True
        End If
End Sub
Wade73
What about a negative integer?
xpda
Thanks a lot.After checking whether it is integer or not, I will check the range.
ssm
If you need to allow negative numbers, then handle the textbox1.validate event and check the whole entry, versus each keypress.
Wade73
+1  A: 

I would consider using a NumericUpDown control instead of a TextBox. It does what you want and has arrows the user can click on to raise or lower the value.

xpda
Thanks. I really didnt thought about that. SSM
ssm