A summary of several common methods for implementing shortcut keys in C #

Source: Internet
Author: User

Shortcut keys is a common function of many software, this article explains three ways to implement C # button shortcut keys, such as ALT + * (button shortcut), ctrl+* and other key combinations. Details are as follows:


First, the C # button shortcut key: Alt + * (Button shortcut key)

When you set the Text property for a button, label, MenuStrip, and so on, add the & key name after the name, such as button1.text= "OK (&O;)". There will be shortcut keys, press Alt+o to execute the button click event.


Second, C # button shortcut key: ctrl+* and other key combinations

In WinForm, set the KeyPreview (Register keyboard event to the form) property to be true for the form to use the key combination.

It then uses the form's KeyDown event (which occurs when a key is first pressed).

The C # button shortcut key instance code is as follows:

private void ***_keydown (object sender, KeyEventArgs e) {if (E.keycode = = keys.f && e.control) {button1. PerformClick (); Perform the action of clicking Button1}}


Note here:

1, * * * represents the form name, you can look at the "Keys" enumeration parameters to achieve their own needs

2. There is also a problem, when using CTRL + * shortcut keys, when the focus on a writable control (such as a TextBox), the * key value may be entered simultaneously, you need to add another sentence to set handled to True to cancel the KeyPress event.

That

private void ***_keydown (object sender, KeyEventArgs e) {  if (E.keycode = = keys.f && e.control)  {    E. Handled = true;  Set handled to true to indicate that the KeyPress event button1 has been processed    . PerformClick ();  }}


Third, C # button shortcut key method

Or take the button as an example. Add a contextMenuStrip1 to the form, set it to the button, and assume it is button1. Add an item to CONTEXTMENUSTRIP1, then set the shortcut key for it (which is the shortcut you want to add to the button), and set its Visible property to false. This enables the C # button shortcut key to be set successfully.


Iv. Fourth method of the C # button shortcut key

protected override bool processCmdKey (ref Message msg, Keys keyData) {if (KeyData = = (Keys.escape)) {this   . Close (); } return base. processCmdKey (ref msg, keyData);}

It is hoped that the method described in this paper can be used for reference and help for the C # program design of everyone.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
A summary of several common methods for implementing shortcut keys in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23659.html






Related content using serialization to implement a deep copy of a list instance (recommended) C # Implementation of ADSL automatic disconnection and dialing method (for dial-up users) windows in the C # call to dllc# operations local files and the basic method of saving files to the database summary
WinForm Form Effect Instance analysis C # methods for searching text to appear in files and folders C # to determine whether a partition is NTFS-formatted the timer and dispatchertimer usages in C #

C # A summary of several common methods for implementing shortcut keys

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.