473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

KeyDown Event on a ListBox

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
2 7219
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bardo | last post by:
I have a Datagrid that is inside a panel. I want to use the keyDown event to reconize the arrow keys. But I have no luck at all. The problem is that the keydown event won't fire at all, unless I...
4
by: Anne | last post by:
hie again, i have 3 textbox and i would like the user to go to the next textbox by pressing the 'ENTER' key. i have tried using this: Private Sub txtRequestor_KeyDown(ByVal sender As...
4
by: ShaneO | last post by:
I would like to handle the KeyUp & KeyDown events in the same event handler but can't find how to determine which event was fired - Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As...
1
by: fripper | last post by:
I have a VB 2005 windows app and I want to recognize keydown events. I have a form key down event handler but it does not get control when a key is depressed. In playing around I found that if I...
0
by: tony | last post by:
Hello!! I have a derived class called StringClassEditor which inherit from UITypeEditor listed below. Now to my question in method EditValue in this class I have this statement lb.KeyDown ...
3
by: MLM450 | last post by:
I have a control that handles the KeyDown event but it does not seem to execute when a combination of keys is pressed - like CTRL+Z. If I press CTRL, it executes. If I press Z, it executes. But the...
3
by: Luc The Perverse | last post by:
Hey - I am making my first C# form application! (More a teach myself app than any pratical application) I have a listbox that always has the control so I made an event handler for KeyDown. I...
3
by: win | last post by:
when the cursor is in a textbox, only coding in the keydown event of the textbox triggered, the coding in the keydown event of the form does not triggered! Problem: I need to change a VB6 program...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.