I have a form with a readonly textbox, some layoutpanels with some buttons in it and together they make up a calculator. (Yes I know this is YAC (yet another...)) but I have fun while still learning!
When I click the buttons and manipulate the Text property of the textbox, al is well.
Now I like to use keyboard input also, but I don't seem to get a grab at the KeyDown event. My textbox obviously does not have Focus, but which control has?
What am I doing wrong or how should I proceed?

7 Years
Discussion Span
Last Post by ddanbe

Are you using the form's KeyDown or the control's KeyDown event?

Since this is a calculator i'm guessing you want to catch when a user presses "*" regardless of what control has focus to handle the event. In this case you should handle the form's KeyDown event.

Also you need to set KeyPreview on the form to true so you can intercept the event before it reaches the focused control.

Form.KeyPreview = true;
Votes + Comments
Has great knowledge!

Thanks Scott! Your suggestion is invaluable!
I was thinking of wandering through all the controls off the form to see who has the focus, grab the keydown and continue from there. VERY UGLY, VERY BAD!
Thanks so much.


You're welcome, happy coding!
I'm sure the source would make a great teaching tool for users on the forum when you finish it up ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.