08-18-2007 11:11 PM
08-19-2007 06:37 PM
08-20-2007 12:40 PM
08-20-2007 01:09 PM - edited 08-20-2007 01:09 PM
Hi Dufei,
The behavior you are seeing is normal behavior with regards to how the KeyPress
event works in .NET. Every .NET control and Windows Form have the KeyPress
event you can capture.
Suppose that you have dropped a control onto your form and created and event
handler to handle the KeyPress event. You also have an event handler for the
Forms KeyPress event. Here are behaviors you will see:
- By default, when you run the application and press a key, the control's
KeyPress event will fire but not the Forms event. This is because the control
has focus. If you read the KeyPress event description, it states "Occurs
when the control has focus and the user presses and releases a key". Thus,
if you set the TabStop property for the control to false, meaning that
control will never have focus, then the Forms event will fire.
- Now, Windows Forms have a KeyPreview
property that determines whether the form will receive key events before the
event is passed to the control that has focus. If you set that property to true, both events will fire with the Forms event
coming first. If you don't want the control's event to fire after the Forms
event, inside the Forms event handler, you can set the e.Handler
property to true which controls whether the control's event is
handled.
Hope this clarifies things!
Best Regards,
Message Edited by Jonathan N on 08-20-2007 01:09 PM
08-20-2007 02:16 PM