Two ways to toggle the focus of the control in C # with the arrow keys and enter key

Source: Internet
Author: User
Environment: There are textbox,combobox and other controls on the interface.

It is not recommended to use the left and RIGHT arrow keys to switch focus, or you can change the character position of the cursor inside the textbox is inconvenient.

Method One: A stupid method, you need to register event handling separately for each control

In the case of a textbox, the code is as follows:

1 private void Textbox_keydown (object sender, KeyEventArgs e)          2 {              3     if (E.keycode = = Keys.down | | e.keycode = = Keys.enter)              4     {                  5         e.suppresskeypress = true;                  6         System.Windows.Forms.SendKeys.Send ("{tab}");              7     }              8     else if (E.keycode = = keys.up)              9     {                 ten         e.suppresskeypress = true;         System.Windows.Forms.SendKeys.Send ("+{tab}");         13}

Method Two: A simple method, you do not need to register event handling for each control separately, just add the following code on the form class:

1//Up and DOWN ARROW keys, and enter key toggle control Focus 2 protected override bool processCmdKey (ref Message MSG, keys KeyData) 3 {4     keys key = (keyData & Keys.keycode); 5     if (E.keycode = = Keys.down | | e.keycode = keys.enter              )     6                                   {7     System.Windows.Forms.SendKeys.Send ("{tab}");  8     return true;             9     }             the     else if (E.keycode = = keys.up) One     {     System.Windows.Forms.SendKeys.Send ("+{tab}");     return true;     return base. processCmdKey (ref msg, keyData); 16}

In this case, the ability to toggle the focus of the control has been implemented, and now there is a new requirement that there are two ComboBox controls Cmbmeas and Cmbremark on the form interface, and I want to commit on the two controls on enter, instead of switching focus, what should I do? It is necessary to determine whether the current focus control is Cmbmeas or Cmbremark, the above code needs to be slightly changed, the code is as follows:

 1//api declaration: Gets the current focus control handle 2 [DllImport ("user32.dll", CharSet = CharSet.Auto, callingconvention = Callingconvention.winap i)] 3 internal static extern IntPtr GetFocus (); 4 5//Gets the control that currently has focus 6 private control Getfocusedcontrol () 7 {8 control Focusedcontrol = NULL; 9//To get hold O f the focused control:10 IntPtr Focusedhandle = GetFocus (); one if (focusedhandle! = IntPtr.Zero)//foc      Usedcontrol = Control.fromhandle (focusedhandle); Focusedcontrol = Control.fromchildhandle (focusedHandle); 14 return Focusedcontrol;}16 protected override bool processCmdKey (ref Message msg, Keys keyData). {Keys Ke y = (KeyData & keys.keycode); Control ctrl = Getfocusedcontrol (); if (E.keycode = = Keys.down | | (Key = = Keys.enter && Ctrl. Name! = "Cmbmeas" && Ctrl. Name = "Cmbremark")) ({System.Windows.Forms.SendKeys.Send ( "{tab}");      return true;      +-Else if (E.keycode = = keys.up) 27 {28             System.Windows.Forms.SendKeys.Send ("+{tab}"); return true; To return to base. processCmdKey (ref msg, keyData); 32}

Description

Control.fromhandle method

Returns the control currently associated with the specified handle, or returns a null reference if the control with the specified handle is not found.

Control.fromchildhandle method

If you need to return a control that has more than one handle, you should use the fromchildhandle method.

This method searches up the parent chain of the window handle until it finds a handle associated with the control. This method is more reliable than the FromHandle method because it correctly returns controls that have multiple handles.

For user-defined controls, you should use the Fromchildhandle method.

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.