Click here to Skip to main content
13,248,369 members (37,836 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello ,

i have read the key press, key down , key up event but i couldn't test it by any code example ... may u help me please for a simple code of this events ...?

i tried it but not working when i press Enter Key? :(

private void textBox1_Keydown(object sender, KeyPressEventArgs e)
           if (e.KeyChar == (char)Keys.Enter)
               string s = textBox1.Text;
     "Enter is down " + s);

many thanks
Posted 7-Dec-12 8:33am
Updated 7-Dec-12 8:43am
lida zar 7-Dec-12 14:12pm
and how keu up event work then?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

For a KeyDown event, you've a KeyEventArgs as a parameter, not a KeyPressEventArgs.
You should use a KeyPressEventArgs as a parameter for the KeyPress method.
Set the parameter to a KeyEventArgs (or use a KeyDown event instead of a KeyPress event) and try this:
private void textBox1_Keydown(object sender, KeyEventArgs e)
    if (e.KeyCode == Keys.Up) // or Keys.Down, Keys.Left, Keys.Right
         // do something...

Hope this helps.
lida zar 7-Dec-12 13:57pm
thanks and how use key up event?
Nelek 7-Dec-12 16:08pm
The same as key down, the difference is when they are trigger. One is when you put your finger on it, the other is when you move your finger off.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100