KeyDown Event on a ListBox

W

Will Gillen

I am building a hoem theatre app that is controlled by an IR Remote Control.
The Remote has some keys that map to "keyboard" commands (i.e. PageUp,
PageDown.).

In this VB.NET application I have a listbox control that I fill with songs
from the MediaPlayer Library (MediaCollection).
Now, my remote does not have "Tab" key or "up" and "down" arrow keys, but
it does have pageup, pagedown. I want the PageUp, PageDwn to "behave" like
the "up" and "down" arrow keys.

To do this, I tried the following:

Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown
TextBox1.Text = e.KeyCode

Dim i As Integer = ListBox1.SelectedIndex
If e.KeyCode = Keys.PageDown Then
i = i + 1
If i > ListBox1.Items.Count - 1 Then
i = 0
End If
ListBox1.SelectedIndex = i
End If
End Sub


And similar code in there for the Keys.PageUp


The Problem is:

Even though the event is being fired, and the SelectedIndex is being changed
based on the If Statement, the "PageUp" command still executes after this
event handler completes. For instance, if the ListBox1 has 15 items, and
the SelectedIndex = 0 at the beginning. When the PageDown Key is pressed,
the even handler changes the SelectedIndex to 1. But, then immediately
after that, the Selected Index becomes 14 (because the PageDown command
processes).

Is there a way to "swallow" the PageUp/PageDown inside the KeyDown Event
Handler that I have written.

I am trying to avoid having to do a "Low Level Keyboard" Lock with the API.

Thanks.

-- Will Gillen
-- Gradekit.com
 
C

cbDevelopment

Use the Handled property of the keyEventArgs of the KeyPress Event. If you
say e.handled=true, then .NET assumes you did whatever behavior needed to
be done. So you do your work in KeyDown and use e.handled=true in KeyPress
to prevent the listbox from processing the key.

Best of luck!
 
W

Will Gillen

Thank you. That worked perfectly!


cbDevelopment said:
Use the Handled property of the keyEventArgs of the KeyPress Event. If you
say e.handled=true, then .NET assumes you did whatever behavior needed to
be done. So you do your work in KeyDown and use e.handled=true in KeyPress
to prevent the listbox from processing the key.

Best of luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top