|
-
Jan 15th, 2005, 07:47 AM
#1
Thread Starter
Member
VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%^&
Hey all
As i said in Title, how do u stop symbols being entered into a textbox??
-
Jan 15th, 2005, 08:17 AM
#2
Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%^&
You could catch them in the keypress event.
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim strInvalid As String
strInvalid = "/#~*/-*()&^&%^& "
If InStr(strInvalid, Chr(KeyAscii)) > 0 Then
KeyAscii = 0
End If
End Sub
You sould also come up with a routine that remove these characters when focus moves form the control in case the text was pasted into the textbox.
-
Jan 15th, 2005, 09:50 AM
#3
Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%^&
The only thing I would change here is to split it off so the function can be used with other text boxes:
Code:
Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = ValidateKeyPress(KeyAscii)
End Sub
Private Sub txtWoof_KeyPress(KeyAscii As Integer)
KeyAscii = ValidateKeyPress(KeyAscii)
End Sub
Private Function ValidateKeyPress(ByVal pintKeyAscii As Integer) As Integer
Const INVALID_CHARS As String = "/#~*-*()&^%"
If InStr(INVALID_CHARS, Chr$(pintKeyAscii)) > 0 Then
ValidateKeyPress = 0
Beep
Else
ValidateKeyPress = pintKeyAscii
End If
End Function
Woof
Last edited by Wokawidget; Jan 15th, 2005 at 10:07 AM.
-
Jan 15th, 2005, 09:56 AM
#4
Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%
or the other way to do it is just have a list of characters that are allowed, otherwise you might still miss some like ½ ¼ ¾
you can get all the standard alpha with there ascii value
try this
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 32, 65 To 90, 97 To 122, 48 To 57
Case Else
KeyAscii = 0
End Select
32 is space, 65 -90 is A - Z, 97 -122 is a - z and 48 - 57 is "0 - 9"
it still will not pick up characters pasted in
rgds pete
-
Jan 15th, 2005, 12:52 PM
#5
Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%
how would you ban the enter button from being pressed? {enter} ?
also how do you only let it "print" something once?so it doesnt loop
-
Jan 15th, 2005, 01:08 PM
#6
Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'Prevent the use of the "enter" key
If KeyAscii = 13 Then KeyAscii = 0
End Sub
What do you mean by "print"?
-
Jan 15th, 2005, 02:34 PM
#7
Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%^&
you would have to check each one, and when you add it, mark that it is added for the next search. There are a lot of choiced, but if you use keypress, you can search 255 only.
-
Jan 15th, 2005, 03:28 PM
#8
Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%
Code:
Private Sub cmdclicker1_Click()
Dim KeyAscii As Integer
If KeyAscii = 13 Then KeyAscii = 0
Timer1.Enabled = True
txtbox1.Text = CStr(Val(txtbox1.Text) + 1)
If Text1.Text <= 0 Then
Timer1.Enabled = False
enter = 0
End If
End Sub
not working, even though i know i understand how it works
-
Jan 15th, 2005, 06:37 PM
#9
Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%
 Originally Posted by |2eM!x
Code:
Private Sub cmdclicker1_Click()
Dim KeyAscii As Integer
If KeyAscii = 13 Then KeyAscii = 0
Timer1.Enabled = True
txtbox1.Text = CStr(Val(txtbox1.Text) + 1)
If Text1.Text <= 0 Then
Timer1.Enabled = False
enter = 0
End If
End Sub
not working, even though i know i understand how it works
What is it supposed to do? What is the timer for?
Has someone helped you? Then you can Rate their helpful post. 
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
|