How Winform processes the same hotkey of multiple buttons

Source: Internet
Author: User

In Winform, we can use & on the button name to set a hotkey for the button. But what happens if multiple buttons have the same hotkey? Let's create an experiment.

We put three buttons and set the same hotkey. Then, we use MessageBox. Show in the processing function of each button to output our own name, to indicate which button was triggered.

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/2041062139-0.png "border =" 0 "height =" 216 "/>

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/20410C461-1.png "border =" 0 "height =" 213 "/>

After running the program, press Alt + B and you will find the prompt of button2 popped up. After you press OK, there will be no other prompts. For example:

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/20410632M-2.png "border =" 0 "height =" 168 "/>

In this case, let button2 get the focus and press Alt + B to try again. You will receive a prompt from button3. From this we can conclude that Winform searches for the next control that can trigger hot keys based on the current focus position and Tab order. Note that the search order is from the next control of the current focus to the control of the current focus.

Let's do another experiment to create a new user control. In this user control, we add a button and set the hot key to Alt + B. Add a Sequence attribute in the background, add the Sequence value to the button name in the control's Load event, and Show the button name in the button's Click Event to indicate which button is triggered.

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/2041061S8-3.png "border =" 0 "height =" 44 "/>

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/2041061C6-4.png "border =" 0 "height =" 186 "/>

After the user control is completed, we add it to our form, put three, and change the Sequence attribute to 1, 2, 3 in Sequence.

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/2041062943-5.png "border =" 0 "height =" 168 "/>

After running, we will focus on ucbutton1 and press Alt + B. You will find the message indicating uc & button1. The same is true for ucbutton2 and uc & button3.

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/20410B010-6.png "border =" 0 "height =" 191 "/>

Why? Does Usercontorl have different processing methods? Let's start an experiment. Create a new user control, add three buttons to set the same hotkey, and add the background method as follows:

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/2041062E8-7.png "border =" 0 "height =" 139 "/>

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/20410C463-8.png "border =" 0 "height =" 214 "/>

Then add the user control to the form.

650) this. width = 650; "style =" background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; border-top: 0px; border-right: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; "title =" image "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/2041061452-9.png "border =" 0 "height =" 194 "/>

After running the program, place the focus on button3 and press Alt + B. You will receive a prompt from uc2button1. The above conclusion is correct. When you focus on uc2button1, press Alt + B. You will receive a message from uc2button2. When you focus on uc2button3, press Alt + B. You will receive a message from uc2button1.

So we can draw a conclusion. After the focus enters usercontrol, Winform searches for the range of the next trigger control, which is limited to this user control. Now, we can understand that the second experiment is about ucbutton1 ~ 3 will trigger the current button, because there is only one control with the hot key Alt + B within the user control range.

However, we normally do not encounter this problem. I encountered this problem when loading the same user control multiple times on a form. I personally think that we should not rely on Winform's search mechanism, but should add more processing to control the trigger logic of hot keys, such as Enable or Visable to ensure that hot keys do not conflict.

This article from the "only text cut through time and space" blog, please be sure to keep this source http://arthurshayne.blog.51cto.com/3491820/1255883

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.