Textbox type fixing-Visual Basic.NET Framework 3.5
I am trying to develop a programe in Visual basic.ne(.NET Framework 3.5) where text开发者_开发问答box will only accept integer. User wont be able to type char/decimel or other type.
Can anyone please help me?
Thanks in advance.
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.
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.
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
精彩评论