Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I code a project C# for testing usercontrol. I add it to the form and it works. But when i use Form_KeyDown event, it doesn't jump into the funtion (I had checked breakpoint).
when I disable "this.Controls.Add(newControl)", it returns to work.
I don't unsderstand where i'm wrong
Please, help me.
Thanks so much.
Posted

Why are you trying to handle keydown in the form?

Try setting the ActiveControl property to null and see if that helps (I'm not sure it will).

Either that, or handle the keydown event in the control and set e.Handled to false in the control's event handler.
 
Share this answer
 
Comments
Slave_hunter_KaKa 15-Nov-10 21:39pm    
Thanks for reply John.
I have problem in the form. I try Form.Active(), or Form.ActiveControl = null.
But bug not yet settled.
Your control has the focus, so no keyboard events will work on the form, UNLESS, you set the form's KeyPreview property to True first.
 
Share this answer
 
Comments
Slave_hunter_KaKa 15-Nov-10 21:43pm    
Thank you, Dave.
I do as you said. But it didn't solve.
Even i can not catch KeyDown, i can handle MouseDown event on the Form.
Slave_hunter_KaKa 16-Nov-10 5:30am    
Dear Dave.
I think i misunderstood. I set keypreview to true and capture keydown successfully.
Thank you so much. :D
#realJSOP 16-Nov-10 5:38am    
My Vote of 5 - This should be marked as the answer.
It does work as explained. KeyPreview tells the form that it gets first look at all keyboard event before they are dispatched to the control that has focus. There's something else you're not telling us about.
 
Share this answer
 

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