Click here to Skip to main content
12,406,098 members (76,261 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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? Frown | :(

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



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

1 solution

Rate this: bad
 
good
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.
  Permalink  
v4
Comments
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 | Mobile
Web01 | 2.8.160726.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2016
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