C # How to Implement shortcuts,

Source: Internet
Author: User

C # How to Implement shortcuts,

This article describes three methods to achieve C # button shortcuts, such as Alt + * (button shortcut), Ctrl + *, and other key combinations.

I,C # one of the buttons: Alt + * (button shortcut)

When you set the Text attribute for buttons, labels, menuStrip, and other controls, add the & Key name behind the name, for example, button1.text = "OK (& O )". The shortcut key is displayed. press Alt + O to execute the button and click the event.

II,C # The second type of button shortcut key: Ctrl + * and other key combinations

In WinForm, set the KeyPreview attribute of the form to use the combination key (register the keyboard event to the form) to True;

Then, use the KeyDown event of the form (which occurs when a key is pressed for the first time ).

Example code for C # button shortcut:

Private void *** _ KeyDown (object sender, KeyEventArgs e) {if (e. keyCode = Keys. F & e. control) {button1.w.mclick (); // execute the action of clicking button1 }}

 

 

C # button shortcut note:

1. *** indicates the form name. You can take a look at the enumeration parameters of "Keys" to implement your own needs.

2. Another problem is that when you use the Ctrl + * shortcut key, * key values may be entered simultaneously for writable controls (such as TextBox) with focus, add another sentence to set Handled to true to cancel the KeyPress event.

That is:

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.20.mclick () has been processed ();}}

 

III,C # The third method of button shortcut

Take the button as an example. Add a contextMenuStrip1 to form, and attach it to the button, which is assumed to be button1. Add an item to contextMenuStrip1, set the shortcut key for it (that is, the shortcut key you want to add to the button), and set its Visible attribute to false. In this way, C # The button shortcut is set successfully.

IV,C # Method 4 of button shortcuts

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

 


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

Related Article

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.