|
-
Dec 10th, 2006, 05:42 AM
#1
Thread Starter
Lively Member
Disable alphabet and special characters
I Hope I Could be Great Like You
-
Dec 10th, 2006, 05:47 AM
#2
Re: Disable alphabet and special characters
Please mark you thread resolved using the Thread Tools as shown
-
Dec 10th, 2006, 05:48 AM
#3
Re: Disable alphabet and special characters
Are you talking about only allowing numeric input into something like a text box?
-
Dec 10th, 2006, 05:51 AM
#4
Thread Starter
Lively Member
Re: Disable alphabet and special characters
I Hope I Could be Great Like You
-
Dec 10th, 2006, 06:00 AM
#5
Re: Disable alphabet and special characters
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.
-
Dec 10th, 2006, 06:22 AM
#6
Thread Starter
Lively Member
Re: Disable alphabet and special characters
wat if i want to code only the textbox?!?
I Hope I Could be Great Like You
-
Dec 10th, 2006, 06:37 AM
#7
Re: Disable alphabet and special characters
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
Please mark you thread resolved using the Thread Tools as shown
-
Dec 10th, 2006, 06:47 AM
#8
Re: Disable alphabet and special characters
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.
-
Dec 10th, 2006, 07:03 AM
#9
Re: Disable alphabet and special characters
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
Please mark you thread resolved using the Thread Tools as shown
-
Mar 8th, 2011, 04:51 PM
#10
New Member
Re: Disable alphabet and special characters
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?
-
Jul 16th, 2011, 07:06 AM
#11
Addicted Member
Re: Disable alphabet and special characters
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
Last edited by levent; Jul 16th, 2011 at 10:26 AM.
-
Jul 16th, 2011, 08:22 AM
#12
Re: Disable alphabet and special characters
 Originally Posted by jeric_mandrake
how will i do that?
 Originally Posted by PrattDiddy
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?
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
-
Jul 16th, 2011, 08:49 AM
#13
Re: Disable alphabet and special characters
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.
-
Jul 16th, 2011, 09:58 AM
#14
Re: Disable alphabet and special characters
 Originally Posted by seenu_1st
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?
-
Jul 16th, 2011, 10:41 AM
#15
Re: Disable alphabet and special characters
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.
-
Jul 16th, 2011, 11:06 AM
#16
Re: Disable alphabet and special characters
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
Last edited by seenu_1st; Jul 16th, 2011 at 11:11 AM.
-
Jul 16th, 2011, 11:48 AM
#17
Re: Disable alphabet and special characters
All that code and more is in my ActiveX control.
-
Jul 16th, 2011, 11:52 AM
#18
Re: Disable alphabet and special characters
 Originally Posted by MartinLiss
All that code and more is in my ActiveX control.
i think i hav used ur ActiveX control b4 some yrs, is this old one or new one u created?
-
Jul 16th, 2011, 02:36 PM
#19
Re: Disable alphabet and special characters
 Originally Posted by seenu_1st
i think i hav used ur ActiveX control b4 some yrs, is this old one or new one u created?
This is the only one but it has been upgraded a number of times.
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
|