-
Jan 9th, 2008, 11:12 PM
#1
Thread Starter
Frenzied Member
Allow NumbersOnly
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.
-
Jan 10th, 2008, 11:19 AM
#2
Re: Allow NumbersOnly
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..
-
Jan 24th, 2008, 04:42 AM
#3
Lively Member
Re: Allow NumbersOnly
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 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..
where is this control???
-
Jan 24th, 2008, 08:05 AM
#4
Re: Allow NumbersOnly
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
-
Jan 31st, 2008, 09:17 PM
#5
Re: Allow NumbersOnly
Originally Posted by Zach_VB6
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.
Are negative numbers or decimal numbers (i.e -123 or 123.56) considered numbers? Your solution won't work for these.
-
Feb 1st, 2008, 11:36 AM
#6
Re: Allow NumbersOnly
Originally Posted by stanav
Are negative numbers or decimal numbers (i.e -123 or 123.56) considered numbers? Your solution won't work for these.
I don't know about the IsNumeric() function, but I do know the NumericUpDown control handles all this gracefully.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|