|
-
Nov 18th, 2003, 02:25 PM
#1
Thread Starter
Hyperactive Member
Prevent characters from being typed
I am trying to program a text box to only accept numbers. I assume you would program the textchanged event of the text box to ignore the keystroke if it is something other than a number? Is that correct or is there a better way to do it?
-
Nov 18th, 2003, 02:36 PM
#2
Hyperactive Member
VB Code:
Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyBack Then Exit Sub
If KeyAscii >= Asc("0") And _
KeyAscii <= Asc("9") Then Exit Sub
KeyAscii = 0
End Sub
-
Nov 18th, 2003, 02:47 PM
#3
Hyperactive Member
You have to be careful with the pasting too. Keypress event doesn't trap the user pasting text in the textbox.
-
Nov 18th, 2003, 03:18 PM
#4
Thread Starter
Hyperactive Member
Thanks,
I tried that and it's not working. Is the vbKeyBack either a function I'm not familiar with or needs to be declared somewhere? I apoligize ahead of time if that's a stupid question. I'm relatively new at this.
-
Nov 18th, 2003, 04:12 PM
#5
You can also click the NumberBox ActiveX control link in my signature. That will get you to code that you can compile to produce an OCX that limits input to numbers. It also allows you to specify maximim decimals and to decide if negative numbers are allowed.
-
Nov 18th, 2003, 04:44 PM
#6
Thread Starter
Hyperactive Member
Thanks Marty,
Since I am new to programming, how can I compile this code and add the control to my toolbox or project?
-
Nov 18th, 2003, 04:47 PM
#7
Just compile it like any other program, save the resulting OCX anyplace you want to (it's probably best to put it where your other OCXs are), go to Project|Components, browse for the new OCX, select it and then just add it to your form like the normal checkbox.
-
Nov 19th, 2003, 10:31 AM
#8
Thread Starter
Hyperactive Member
Please forgive my ignorance, but I'm still not sure how to do that. My only experience is with simple exe programs.
-
Nov 19th, 2003, 10:38 AM
#9
Fanatic Member
Try this
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 47 Or KeyAscii > 57) Then 'Disable keyboard lettering
KeyAscii = 0
End If
end sub
HTH
** HOLLY ** 
-
Nov 19th, 2003, 11:04 AM
#10
Thread Starter
Hyperactive Member
I tried your code, but that still allows me to type anything I want into the text box.
-
Nov 19th, 2003, 11:15 AM
#11
Fanatic Member
I have the same code in one of my app's and it works fine!
are you change the procedure to whatever you have called your
txt box...
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'here the text1 should be whatever you have called your txt box
** HOLLY ** 
-
Nov 19th, 2003, 11:25 AM
#12
Thread Starter
Hyperactive Member
Yes, I cut and pasted your code with the exception of the text box name. Here is my code:
VB Code:
Private Sub txtPNR_KeyPress(ByVal keyascii As Integer)
If (keyascii < 47 Or keyascii > 57) Then
keyascii = 0
End If
End Sub
Still allows me to type anything into txtPNR. Numbers, letters, characters, etc.
-
Nov 19th, 2003, 11:38 AM
#13
I don't know what you've done but the following from your code is invalid.
Private Sub txtPNR_KeyPress(ByVal keyascii As Integer)
The standard textbox KeyPress event sub header would look like
Private Sub txtPNR_KeyPress(KeyAscii As Integer)
-
Nov 19th, 2003, 11:43 AM
#14
Fanatic Member
-
Nov 19th, 2003, 11:47 AM
#15
Lively Member
snippet
visual basic code:
VB Code:
Private Sub cmdSaveRecord_Click()
If txtRegNo.Text = "" Then
MsgBox "Registration number must be entered"
txtRegNo.SetFocus
Exit Sub
ElseIf Not IsNumeric(Me.txtRegNo) Then
MsgBox "You have entered a non-numeric value, please enter a number that is 5 digits in length", vbCritical, "input error"
Me.txtRegNo.SetFocus
Exit Sub
ElseIf Len(Me.txtRegNo) < 5 Then
MsgBox "You have entered a number less than 5 digits. Enter only a 5-digit number", vbCritical, "input error"
Me.txtRegNo.SetFocus
Exit Sub
ElseIf txtFirstName.Text = "" Then
MsgBox "First name must be entered"
txtFirstName.SetFocus
Exit Sub
ElseIf txtLastName.Text = "" Then
MsgBox "Last name must be entered"
txtLastName.SetFocus
Exit Sub
End If
'When all data has been entered into text boxes and you press the command button (save record) it will be written and saved to a file for later viewing, if file isn't thier it will create it
Open "C:\OffDataFile.txt" For Append As #1
Write #1, UCase$(txtFirstName.Text), UCase$(txtLastName.Text), UCase$(txtRegNo.Text), UCase$(cboMonth.Text), UCase$(cboDay.Text), UCase$(cboYear.Text), UCase$(cboClass.Text)
txtFirstName.Text = "" 'data entered into these text boxes will be saved to a file
txtLastName.Text = ""
cboClass.Text = ""
txtRegNo.Text = ""
cboMonth.Text = ""
cboDay.Text = ""
cboYear.Text = ""
Close #1
End Sub
this is more than what you wanted but it is good code to add to your collection!
hope it helps
-
Nov 19th, 2003, 11:48 AM
#16
Lively Member
oops
sorry forgot to make it vb code.
-
Nov 19th, 2003, 11:51 AM
#17
You can always edit it to add the tags like I just did.
-
Nov 19th, 2003, 12:04 PM
#18
Thread Starter
Hyperactive Member
I am using VB.net and when I type the following:
private sub txtPNR_KeyPress (KeyAscii as Integer)
and I press enter, the text automatically changes to:
private sub txtPNR_KeyPress (ByVal KeyAscii as Integer)
??
-
Nov 19th, 2003, 12:07 PM
#19
I wish you had said so earlier. You posted in the Visual Basic forum. I'll move it for you now.
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
|