How can I avoid any Beep from the ListBox.KeyDown ?

The code below (look at the part that deals with left and right arrows) isse beeps when I press left or right arrows… How can I avoid that (beside shutting down the sound…)

This is the relevant code:

[code]If Key = Chr(28) Then // Left Key: move to top
If Me.ListIndex > 0 Then
// NO BEEP PLEASE: this is not an error !
Me.ListIndex = Me.ListIndex - 1
// * far below
End If

ElseIf Key = Chr(29) Then // Right Key: move to bottom
If Me.ListIndex < (Me.ListCount - 1) Then
// NO BEEP PLEASE: this is not an error !
Me.ListIndex = Me.ListIndex + 1
// * far below
End If
End If
[/code]

Extracted from the ListBox KeyDown Event:

[code]Function KeyDown(Key As String) As Boolean
//
// Allows to Edit the selected Row with Enter or Return pressed keys
// and change the Row selection if the left key (top) or the right key (bottom) are pressed.
//
Dim SelRow As Integer // To hold the selected Row #

// Do nothing if no Row is selected
If Me.SelCount < 1 Then
Return False
End If

// Get the selected Row #
SelRow = LB.ListIndex

// Change the Selected Row to the previous (Left Arrow) or next (Right Arrow) Row
If Key = Chr(28) Then // Left Key: move to top
If Me.ListIndex > 0 Then
Me.ListIndex = Me.ListIndex - 1
End If

ElseIf Key = Chr(29) Then // Right Key: move to bottom
If Me.ListIndex < (Me.ListCount - 1) Then
Me.ListIndex = Me.ListIndex + 1
End If
End If

If (Key = Chr(4) Or Key = Chr(13)) Then
// Edit the selected Row
Me.EditCell(SelRow, 1)

// I handled these characters (both: Enter and Return)
Return True

End If
End Function[/code]

TIA.

  • I can add an Else clause here and jump to the top (if I the current is the last Row) or bottom (if the current Row is the first Row) of the ListBox.

Return True.

You must return True to avoid the beep.

Thank you for the answers.

I cannot do that because I have other code that have to be fired.

I am unsure if putting Return True at the end of the Event will do the trick. Is it that what you meant ?

[quote=176302:@Emile Schwarz]Thank you for the answers.

I cannot do that because I have other code that have to be fired.

I am unsure if putting Return True at the end of the Event will do the trick. Is it that what you meant ?[/quote]
You certainly could return True at the end of the event. Keep in mind though that you are telling he OS that you are handling the key event and it will not be sent to any other interested controls. If you want to stop. The beeps for just the Up and Down keys (and you are not handling them elsewhere) then just return true if the keys are Up or Down.

Hi Greg,

thanks for the advice, I will do that.