Click here to Skip to main content
11,645,087 members (63,761 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
I am trying to capture Tab key on KeyDown or KeyPress But its not working So i use bellow code to capture the Tab key

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
 if (keyData == Keys.Tab)
    MessageBox.Show("Tab KEy Pressed");
 return base.ProcessCmdKey(ref msg, keyData);

This Code is woking..Now i am trying to call keydown event of a control from above code
i try following code
   Control c =this.ActiveControl;
   c.KeyDown+=new KeyEventHandler(c.KeyDown);
but it is not woking...
Pls Help
Posted 18-Nov-10 22:44pm
Edited 18-Nov-10 22:49pm
Abhinav S401.8K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The code you say must call the keydown event looks more like you add the event handler of the control twice.
Control c =this.ActiveControl;
c.KeyDown+=new KeyEventHandler(c.KeyDown);

You should add a event handler to each control which has the following signature:
private void Control_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

You can add this handler to a control like this:
c.KeyDown+=new KeyEventHandler(Control_KeyDown);

By the way, the ProcessCmdKey is overriding an internal routine of a control and is not an event handler. Those are two separate things.

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

Solution 2

Hi, Mr. Nijboer,

Could you please tell me how can I capture the Ctrl key ? I want to check if the Ctrl key is pressed when Mouse Down.
Thank you.
André Kraak at 17-Feb-13 7:35am
I think you intended to comment to a solution, but created a solution instead.

If you have a question about or comment on a given solution use the "Have a Question or Comment?" option beneath the solution. When using this option the person who gave the solution gets an e-mail message and knows you placed a comment and can respond if he/she wants.
Please move the content of this solution to the solution you are commenting on and remove the solution.

Thank you.

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

  Print Answers RSS
0 OriginalGriff 700
1 494
2 Sergey Alexandrovich Kryukov 449
3 Afzaal Ahmad Zeeshan 444
4 CPallini 426
0 OriginalGriff 1,065
1 Sergey Alexandrovich Kryukov 703
2 Afzaal Ahmad Zeeshan 702
3 DamithSL 681
4 CPallini 595

Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2015
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