C # switch the focus of the control between the direction key and the Return key,

Source: Internet
Author: User

C # switch the focus of the control between the direction key and the Return key,

Environment: controls such as TextBox and ComboBox are provided on the interface.

We do not recommend that you use the left and right direction keys to switch the focus. Otherwise, it is inconvenient to change the character position of the cursor in the TextBox.

Method 1: This is a stupid method. You must register an event for each control to handle it.

Take TextBox as an example. 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     {                 10         e.SuppressKeyPress = true;                 11         System.Windows.Forms.SendKeys.Send("+{Tab}");             12     }         13 }

Method 2: A simple method. You do not need to register an event for each control. You only need to add the following code to the form class:

1 // the up and down arrow Keys, and the Enter key switch 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} 10 else if (e. keyCode = Keys. up) 11 {12 System. windows. forms. sendKeys. send ("+ {Tab}"); 13 return true; 14} 15 return base. processCmdKey (ref msg, keyData); 16}

Now, the function of switching control focus has been implemented, and there is a new requirement. The form interface has two ComboBox controls, cmbMeas and cmbRemark, what should I do if I want to submit it when I press Enter on the two controls, instead of switching the focus? You need to determine whether the current control with focus is cmbMeas or cmbRemark. The above code needs to be slightly changed. The specific code is as follows:

1 // API Declaration: Get the current focus control handle 2 [DllImport ("user32.dll", CharSet = CharSet. auto, CallingConvention = CallingConvention. winapi)] 3 internal static extern IntPtr GetFocus (); 4 5 // obtain the current focus Control 6 private Control GetFocusedControl () 7 {8 Control focusedControl = null; 9 // To get hold of the focused control: 10 IntPtr focusedHandle = GetFocus (); 11 if (focusedHandle! = IntPtr. zero) 12 // focusedControl = Control. fromHandle (focusedHandle); 13 focusedControl = Control. fromChildHandle (focusedHandle); 14 return focusedControl; 15} 16 17 protected override bool ProcessCmdKey (ref Message msg, Keys keyData) 18 {19 Keys key = (keyData & Keys. keyCode); 20 Control ctrl = GetFocusedControl (); 21 if (e. keyCode = Keys. down | (key = Keys. enter & ctrl. name! = "CmbMeas" & ctrl. Name! = "CmbRemark") 22 {23 System. windows. forms. sendKeys. send ("{Tab}"); 24 return true; 25} 26 else if (e. keyCode = Keys. up) 27 {28 System. windows. forms. sendKeys. send ("+ {Tab}"); 29 return true; 30} 31 return base. processCmdKey (ref msg, keyData); 32}

Note:

Control. FromHandle Method

Returns the control associated with the specified handle. If no control with the specified handle is found, an empty reference is returned.

Control. FromChildHandle Method

To return a control with multiple handles, useFromChildHandleMethod.

This method searches up the parent chain of the window handle until the corresponding handle is found. This method is similarFromHandleThe method is more reliable because it correctly returns controls with multiple handles.

The FromChildHandle method should be used for custom controls.

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.