Results 1 to 9 of 9

Thread: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%^&

  1. #1

    Thread Starter
    Member ThePCMan's Avatar
    Join Date
    Dec 2004
    Location
    Aylesbury, UK
    Posts
    33

    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??

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%^&

    You could catch them in the keypress event.
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. Dim strInvalid As String
    3.  
    4.     strInvalid = "/#~*/-*()&^&%^& "
    5.    
    6.     If InStr(strInvalid, Chr(KeyAscii)) > 0 Then
    7.         KeyAscii = 0
    8.     End If
    9. 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.

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. Select Case KeyAscii
    3.     Case 32, 65 To 90, 97 To 122, 48 To 57
    4.     Case Else
    5.         KeyAscii = 0
    6. 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

  5. #5
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. 'Prevent the use of the "enter" key
    3. If KeyAscii = 13 Then KeyAscii = 0
    4. End Sub
    What do you mean by "print"?

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  8. #8
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB6 - How to stop characters from being entered into a textbox i.e. /#~*/-*()&^&%

    Quote 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
  •  



Click Here to Expand Forum to Full Width