WinForm Control Development Summary (12) Let control handle navigation keys

Source: Internet
Author: User

Recently really really too busy, so that one months did not yo update my blog. Last night, an online friend read my toolbox article, asked me a question, he said how to make the Toolbox control can also respond to keyboard operation, that is, use the Up,down button to select the item in the Toolbox control, he added keyboard events, but does not work. The first time to do this control is also just a demonstration of the control of the production process, only a very short time to do one, only to consider using the mouse to select, did not consider the keyboard operation, I want to add keyboard operation is nothing more than overloading KeyDown events, for Up,down do some response on it. But after adding the overloaded onkeydown event, the result and that friend said, no effect, I set a point, debugging, found that KeyDown can not capture up at all, down button clicks, what is the reason, is not forget to set the style of control so that it can get focus? So, I used the statement:

SetStyle (controlstyles.selectable, true); still no effect, when we press down on the control, another control gets the focus. At this point the Up,down button just plays the role of navigation like the TAB key.

Next, I put a ListBox control on the test project's form to make a comparison, in fact, Toolbox and the ListBox have similarities in the interface performance, that is, there are child item, and click on the listbox is working, the listbox did not lose focus , which means that the Up,down key does not become the navigation key. I think windows must have a default handle on the default navigation key up,down,left,right unless you want your control to want to handle the keys yourself. Look at the source code of the Listboxcontrol control with the Disassembly tool and discover an interesting function:

protected override bool isInputKey (Keys keyData)

{

if (keyData & keys.alt) = = Keys.alt)

{

return false;

switch ((KeyData & Keys.keycode))

{case

keys.prior: Case

Keys.next:

Case Keys.end: Case

Keys.home: Return

true;

Return to

base. isInputKey (KeyData);

}

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.