The easiest way to add Form-level shortcuts to WinForms programs

Source: Internet
Author: User
Form. keyDown, KeyUp, and KeyPress cannot be used when you add shortcuts to a program, because some controls intercept key information. For example, if you put a Button on the Form and move the focus to it, press any key, Form. keyXXX is not triggered.

To solve this problem, the formal method is to override the Form. ProcessCmdKey method, which is called before the control processes the key information. However, this method is not applicable if it is more convenient and flexible to process shortcuts-for example, it is processed by another class. At this time, the best way is:

Add an additional menu to the form, design the Click Event processor for the menu item, set the ShortcutKeys attribute for it, and set the Visible of the menu to false.

Note that if you have more than one menu in one form, and the shortcut keys of different menu items are repeated, at this time, the event processor with at most one menu item will be called, specifically, the menu item is added to the DropDownItems of the parent menu item (whether the parent menu item is added to another parent control or not !) Search, the first Enable is a real menu item, and its event processor is called. This priority has nothing to do with whether the menu is visible or not, but is related to Enable.

Ps: When Microsoft Debug Symbols + Reflector is used for manual debugging, several lines of code are found. If a WinForms process attaches a debugger, in this program, press Ctrl + Break to use Debuger to freeze the program.

Pps: Is the source code of. Net Framework still unavailable? According to Scott, VS 2008 Express is set, and only Symbols is returned. The source code is still unavailable. If you want to switch to Microsoft's code, you will be prompted whether to display the Assembly :(

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.