How do I get form forms to receive KeyDown events?

Source: Internet
Author: User

Sometimes there is a need to respond to a form's key message when writing a form application using the. Net framework. When there are no other controls on the form, the form can respond directly to those messages.

However, when there are other controls on the form, it is discovered that the form will no longer respond to these messages because the messages are processed by the controls on them and are not sent to the parent form.

It is necessary to respond to key messages on the form. The form class has a KeyPreview property that allows it to receive keystroke messages.

This value indicates whether the form will receive this key event before the key event is passed to the control that has the focus. True if the form will receive all key events, or False if the currently selected control on the form receives a key event. The default is Alse.

When this property is set to True, the form receives all KeyPress, KeyDown, and KeyUp events. After the form's event handler finishes processing the keystroke, it then assigns the keystroke to the control that has the focus. For example, if the KeyPreview property is set to True and the currently selected control is a TextBox, the TextBox control receives the pressed key after the form's event handler has processed the keystroke. To handle keyboard events at the form level only and not allow the control to receive keyboard events, set the Keypresseventargs.handled property in the form's KeyPress event handler to True.

You can use this property to handle most keystroke events in your application, and you can handle keystroke events or invoke the appropriate controls to handle keystroke events. For example, when your application uses function keys, you might want to process these keystrokes at the form level instead of writing code for each control that might receive a keystroke event.

Note that if the form has no visible or enabled controls, the form automatically receives all keyboard events.

How do I get form forms to receive KeyDown events?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.