Home  >  Article  >  Backend Development  >  Two methods for switching control focus using the arrow keys and the Enter key in C#

Two methods for switching control focus using the arrow keys and the Enter key in C#

零下一度
零下一度Original
2017-06-23 16:24:452912browse

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, otherwise it will be inconvenient for you to change the character position of the cursor in the TextBox.

Method 1: Stupid method, you need to register event processing separately for each control

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: Simple method, no need to register event processing separately for each control, just add the following code to the form class:

 1 //上、下方向键,及回车键切换控件焦点 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 }

At this point, the function of switching the focus of the control has been implemented. Now there is a new requirement. There are two ComboBox controls cmbMeas and cmbRemark on the form interface. I want to submit when Enter is pressed on these two controls instead of switching the focus. then what should we do? Then you need to determine whether the control that currently has focus is cmbMeas or cmbRemark. The above code needs to be slightly modified. The specific code is as follows:

 1 //API声明:获取当前焦点控件句柄       2 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)] 3 internal static extern IntPtr GetFocus(); 4  5 //获取当前拥有焦点的控件 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 }

Instructions:

Control.FromHandle method

Returns the control currently associated with the specified handle; if no control with the specified handle is found, a null reference is returned.

Control.FromChildHandle method

If you need to return a control with multiple handles, you should use the FromChildHandle method.

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

For user-defined controls, the FromChildHandle method should be used.

The above is the detailed content of Two methods for switching control focus using the arrow keys and the Enter key in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn