Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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? :(

C#
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
Updated 7-Dec-12 7:43am
v4
Comments
lida zar 7-Dec-12 14:12pm    
and how keu up event work then?

1 solution

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:
C#
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.
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900