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?

Recommended Answers

All 4 Replies

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;
commented: Has great knowledge! +4

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 ;)

No problem!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.