Results 1 to 6 of 6

Thread: [RESOLVED] CTRL+A - Select All: Beep sound?

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Resolved [RESOLVED] CTRL+A - Select All: Beep sound?

    Is there a way to get rid of that beep sound when using CTRL+A to select all text in a textbox?

    I'm using this code, with the Form's KeyPreview property set to True.

    vb Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.    
    3.     If Shift = vbCtrlMask Then
    4.        
    5.         If KeyCode = vbKeyA Then
    6.             Control_SelectAll Me.ActiveControl
    7.         End If
    8.    
    9.     End If
    10.    
    11. End Sub
    12.  
    13. Public Sub Control_SelectAll(ByRef ControlObject As Control)
    14.    
    15.     On Error Resume Next
    16.    
    17.     With ControlObject
    18.         .SetFocus
    19.         .SelStart = 0
    20.         .SelLength = Len(.Text)
    21.     End With
    22.    
    23. End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: CTRL+A - Select All: Beep sound?

    Try setting KeyCode and Shift to 0 after calling your routine, as that will indicate that you have dealt with the key press.

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: CTRL+A - Select All: Beep sound?

    You need to handle the KeyPress event as well. It occurs in addition to the KeyDown and KeyUp events but allows you to cancel a key stroke by setting it's KeyAscii argument to 0.

    The KeyAscii value of a Ctrl+Letter key is from 1(A) to 26(Z)

    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
        If TypeOf Me.ActiveControl Is VB.TextBox Then
            Select Case KeyAscii
                Case 3, 8, 22, 24 'Allowable control keys in a textbox ctrl-C, ctrl-H (backspace), ctrl-V, ctrl-X
                Case Is < 27
                    KeyAscii = 0
            End Select
        End If
    End Sub
    Last edited by brucevde; Jul 24th, 2009 at 03:58 PM.

  4. #4

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: CTRL+A - Select All: Beep sound?

    Quote Originally Posted by si_the_geek View Post
    Try setting KeyCode and Shift to 0 after calling your routine, as that will indicate that you have dealt with the key press.
    Yeah, I tried that first and didn't work.

    Quote Originally Posted by brucevde View Post
    You need to handle the KeyPress event as well. It occurs in addition to the KeyDown and KeyUp events but allows you to cancel a key stroke by setting it's KeyAscii argument to 0.

    The KeyAscii value of a Ctrl+Letter key is from 1(A) to 26(Z)

    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
        If TypeOf Me.ActiveControl Is VB.TextBox Then
            Select Case KeyAscii
                Case 3, 8, 22, 24 'Allowable control keys in a textbox ctrl-C, ctrl-H (backspace), ctrl-V, ctrl-X
                Case Is < 27
                    KeyAscii = 0
            End Select
        End If
    End Sub
    Thanks. I just added the If KeyAscii < 27 part and it fixed it.

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [RESOLVED] CTRL+A - Select All: Beep sound?

    I just added the If KeyAscii < 27 part and it fixed it.
    Just to be sure you understand that without the Case 3,8,22,24 line the user won't be able to delete anything using the Backspace key nor do any Cut/Paste/Copy operations. Those keys will not cause a Beep because they are valid keystrokes in the TextBox.

    If that is allright then

  6. #6

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: [RESOLVED] CTRL+A - Select All: Beep sound?

    Quote Originally Posted by brucevde View Post
    Just to be sure you understand that without the Case 3,8,22,24 line the user won't be able to delete anything using the Backspace key nor do any Cut/Paste/Copy operations. Those keys will not cause a Beep because they are valid keystrokes in the TextBox.

    If that is allright then
    Hm, I had no idea about the 3, 22, or 24. Thanks.

Tags for this Thread

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