Replace Text1 with your textbox.Code:Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress If (IsNumeric(e.KeyChar)) = False Then e.KeyChar = vbNullString End Sub
Replace Text1 with your textbox.Code:Private Sub Text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress If (IsNumeric(e.KeyChar)) = False Then e.KeyChar = vbNullString End Sub
Or use the NumericUpDown control, which is a textbox designed to handle numbers.
It's in the same toolbox as the Textbox, Label, Combobox, etc..
Currently using: VS 2010 Ultimate on Win7 Ultimate x64.
CodeBank: All Threads Colors ComboBox Fading & Gradient Form MoveItemListBox/MoveItemListView MultilineListBox MenuButton ToolStripCheckBox Start with Windows
i tried it and it didn't work well something is wrong
i used this code and works fine
Code:Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Text1.TextChanged If (IsNumeric(Text1.Text)) = False Then Text1.Clear() End Subwhere is this control???Or use the NumericUpDown control, which is a textbox designed to handle numbers.
It's in the same toolbox as the Textbox, Label, Combobox, etc..
The NumericUpDown control serves a different purpose it allows you to move the number in the textbox up and down. Where as using the IsNumeric method allows you to enter numbers only.
In any case it is in the toolbox (NumericUpDown) to be more specific in the "Common Controls" node. Just expand it and it should be mid way up that node![]()
If this post was helpful please rate it![]()
![]()
If it wasn't for C, we would be using BASI, PASAL and OBOL.
VB6: [System Uptime] [Extracting WMP Song] [Basic Flood Protection] [RTF Editor]
[Hex/String Conversions] [Reading/Writing INI] [List Files in Directories]
C++: [Mute Audio in Vista]
VB.NET: [Associating a Help File] C#: [One instance of your application]
Are negative numbers or decimal numbers (i.e -123 or 123.56) considered numbers? Your solution won't work for these.Originally Posted by Zach_VB6
I don't know about the IsNumeric() function, but I do know the NumericUpDown control handles all this gracefully.Originally Posted by stanav
Also with the NUD control, you can set a couple of properties to have it look and act just like a TextBox too, so really people need to stop "re-creating" a Numeric Only TextBox, they serve no purpose, and means you have more code to maintain.
Currently using: VS 2010 Ultimate on Win7 Ultimate x64.
CodeBank: All Threads Colors ComboBox Fading & Gradient Form MoveItemListBox/MoveItemListView MultilineListBox MenuButton ToolStripCheckBox Start with Windows