473,387 Members | 1,693 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,387 software developers and data experts.

Trouble With KeyDown Event in Form (Noob)

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 seem to be losing information when Casting to char, since I never have any
problem using special keys if I use something like e.KeyCode == Keys.Delete

But . . there is no Keys.Astericks (or Star or similar)

private void PlayList_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e){
switch((char)e.KeyValue){
case 'x':case 'X':
Close();
break;
case '*':
System.Windows.Forms.MessageBox.Show("I never get here");
break;
default:
//do nothing
break;
}
}
Two things. How am I supposed to test for normal codes like this?

Is there a way to capture things for the entire form without making a
KeyDown handler for every item in the form?

--
LTP

:)
Apr 10 '07 #1
3 7864
On Tue, 10 Apr 2007 11:05:16 -0700, Luc The Perverse
<sl***********************@cc.usu.eduwrote:
[...]
Two things. How am I supposed to test for normal codes like this?
It depends on what you want. The KeyDown event is particular to specific
physical keys on the keyboard. There's no "*" key per se...that's the 8
key with the SHIFT key held down (on a standard keyboard). So to detect
that you'd look for the "8" key, also checking the state of the SHIFT key.

Additionally, you shouldn't be casting the KeyValue property to a char,
because that's not what it is. Simply use the values from the enumeration
in your switch statement. In other words, in your example you'd use
Keys.X and Keys.D8 for the case statements, and in the Keys.D8 statement
you'd only execute the code if the KeyEventArgs.Shift property was true.

Alternatively, if all you really care about is truly the ASCII code
generated by the user, rather than the physical key being pressed, handle
the KeyPress event instead. That does give you the final ASCII code that
results from the keyboard input and you don't have to mess with the
parsing the key+modifier combinations yourself.
Is there a way to capture things for the entire form without making a
KeyDown handler for every item in the form?
As the documentation points out:

To handle keyboard events only at the form level and not
enable other controls to receive keyboard events, set
the KeyPressEventArgs.Handled property in your form's
KeyPress event-handling method to true

In other words, handle the event in the form and mark it as handled so
that it doesn't propogate down to the child controls.

The same thing applies for the KeyDown and KeyUp events, except those of
course use the KeyEventArgs class instead of KeyPressEventArgs class.

Pete
Apr 10 '07 #2
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 10 Apr 2007 11:05:16 -0700, Luc The Perverse
<sl***********************@cc.usu.eduwrote:
>[...]
Two things. How am I supposed to test for normal codes like this?

It depends on what you want. The KeyDown event is particular to specific
physical keys on the keyboard. There's no "*" key per se...that's the 8
key with the SHIFT key held down (on a standard keyboard). So to detect
that you'd look for the "8" key, also checking the state of the SHIFT key.

Additionally, you shouldn't be casting the KeyValue property to a char,
because that's not what it is. Simply use the values from the enumeration
in your switch statement. In other words, in your example you'd use
Keys.X and Keys.D8 for the case statements, and in the Keys.D8 statement
you'd only execute the code if the KeyEventArgs.Shift property was true.

Alternatively, if all you really care about is truly the ASCII code
generated by the user, rather than the physical key being pressed, handle
the KeyPress event instead. That does give you the final ASCII code that
results from the keyboard input and you don't have to mess with the
parsing the key+modifier combinations yourself.
>Is there a way to capture things for the entire form without making a
KeyDown handler for every item in the form?

As the documentation points out:

To handle keyboard events only at the form level and not
enable other controls to receive keyboard events, set
the KeyPressEventArgs.Handled property in your form's
KeyPress event-handling method to true

In other words, handle the event in the form and mark it as handled so
that it doesn't propogate down to the child controls.

The same thing applies for the KeyDown and KeyUp events, except those of
course use the KeyEventArgs class instead of KeyPressEventArgs class.

Pete
private void list_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e){
if(e.KeyChar=='*')
System.Windows.Forms.MessageBox.Show("Hooray!");
}

Um . . . Hooray!

Thanks dude :)

--
LTP

:)
Apr 11 '07 #3
On Tue, 10 Apr 2007 21:38:49 -0700, Luc The Perverse
<sl***********************@cc.usu.eduwrote:
private void list_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e){
if(e.KeyChar=='*')
System.Windows.Forms.MessageBox.Show("Hooray!");
}

Um . . . Hooray!

Thanks dude :)
You're welcome...thanks for letting us know it helped. :)
Apr 11 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Frank T. Clark | last post by:
How do I redirect or capture keydown events in a parent form from a child form? I have a main form which displays another informational form marked "SizableToolWindow". Form child = new...
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...
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...
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: 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...
14
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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.