By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

KeyDown Event on a ListBox

P: n/a
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


Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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!

"Will Gillen" <gi******@nsuok.edu> wrote in
news:ut**************@TK2MSFTNGP15.phx.gbl:
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



Nov 21 '05 #2

P: n/a
Thank you. That worked perfectly!
"cbDevelopment" <de*********@remove.700cb.net> wrote in message
news:Xn***************************@207.46.248.16.. .
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!

"Will Gillen" <gi******@nsuok.edu> wrote in
news:ut**************@TK2MSFTNGP15.phx.gbl:
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


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.