how will i do that?
Printable View
how will i do that?
Where?
Are you talking about only allowing numeric input into something like a text box? :confused:
yup,,
There are a number of ways of accomplishing this, but I think the easiest is to use the Textbox control that Martin Liss created which, by its nature, only allowes numbers and has a few other very cool features.
Check this out.
wat if i want to code only the textbox?!?
This one?
VB Code:
Option Explicit Private Sub Text1_KeyPress(KeyAscii As Integer) 'Check wheather number is entered If IsNumeric(KeyAscii) = True Then 'if yes then allow the entry KeyAscii = KeyAscii Else 'dont allow the entry KeyAscii = 0 End If End Sub
Thread starter, keep in mind that most solutions perform the check during keyboard input. In the end, you will still have to recheck the final input in order to handle inputs through copy+paste.
As per Leinad advice,
I have added this one also
VB Code:
Private Sub Text1_Validate(Cancel As Boolean) If IsNumeric(Text1.Text) = True Then Cancel = False Else MsgBox "Not a Valid Numeric Entry" Cancel = True End If End Sub
I tried using this but when I stepped through and got to IsNumeric(KeyAscii), it always came out to be true even when using letters. I'm trying to only allow numbers in a text box in VB6. Any ideas on accomplishing this?
Hi;
Wellcome to forum
This new code sample which belong to Martin Liss
Code:Private Sub Textbox_KeyPress(KeyAscii As Integer)
' Allow only numbers and delete
Select Case KeyAscii
Case 48 To 57, 8
'okay - do nothing
Case Else
' 'Eat' the input
KeyAscii = 0
End Select
End Sub
When you compile the code in this project it produces my NumberBox ActiveX control that is a textbox that allows only numbers. It also has these additional properties:
CanBeNegative - True/false
CanHaveDecimals - True/False
DecimalSeparator - period/comma
MaxDecimals - maximum number of decimals allowed
MaxValue - highest value allowed
MinValue - smallest number allowed
RequireLeadingDigit - True/False
jeric, u can simply restrict to type only numbers including negative sign, decimal sign, backspace etc.. to the textbox with keypress event as u got examples above, but remember to block the right click function also, since user can paste letters to the textbox.
The trouble with the "simple" code in some of these posts is that it's too simple. Consider the following
You can't paste 123 or any valid number and the user might want to.
Is -123 a number?
Is 123- a number?
Is - 123 a number?
Is 123.123.123 a number?
Is 123..123 a number?
Martin, i dont say it's simple, i just said simply restrict to type only numbers, but need other contrains to be remembered, i mentioned a point, u mentioned some more thats all, i realy agree with u.
try this.. user can input numbers, negative sign, decimal point at right place, check and feed back if any error...
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 45 'negative key
If Text1.Text = "" Or (Text1.SelStart = 0 And InStr(Text1.Text, "-") = 0) Then
KeyAscii = 45 ' negative sign
ElseIf Text1.SelLength = Len(Text1.Text) Then
KeyAscii = 45 ' negative sign
Else
KeyAscii = 0
End If
Case 48 To 57, 8 'numbers and backspace
Case 46 'decimal point
If Text1.SelLength <> Len(Text1.Text) Then 'not fully selected
If InStr(Text1.Text, ".") > 0 Then
KeyAscii = 0
End If
End If
If InStr(Text1.SelText, ".") > 0 Then 'if point in selected text
KeyAscii = 46 ' decimal point
End If
Case Else
KeyAscii = 0
End Select
End Sub
All that code and more is in my ActiveX control.