


Two methods for switching control focus using the arrow keys and the Enter key in C#
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Xiaomi 14Ultra is one of the most popular Xiaomi models this year. Xiaomi 14Ultra not only upgrades the processor and various configurations, but also brings many new functional applications to users. This can be seen from the sales of Xiaomi 14Ultra mobile phones. It is very popular, but there are some commonly used functions that you may not know yet. So how does Xiaomi 14Ultra switch between 4g and 5g? Let me introduce the specific content to you below! How to switch between 4g and 5g on Xiaomi 14Ultra? 1. Open the settings menu of your phone. 2. Find and select the "Network" and "Mobile Network" options in the settings menu. 3. In the mobile network settings, you will see the "Preferred network type" option. 4. Click or select this option and you will see

How to convert Win11 Home Edition to Win11 Professional Edition? In Win11 system, it is divided into Home Edition, Professional Edition, Enterprise Edition, etc., and most Win11 notebooks are pre-installed with Win11 Home Edition system. Today, the editor will show you the steps to switch from win11 home version to professional version! 1. First, right-click on this computer on the win11 desktop and properties. 2. Click Change Product Key or Upgrade Windows. 3. Then click Change Product Key after entering. 4. Enter the activation key: 8G7XN-V7YWC-W8RPC-V73KB-YWRDB and select Next. 5. Then it will prompt success, so you can upgrade win11 home version to win11 professional version.

Many friends may not be used to the win system when they first come into contact with it. There are dual systems in the computer. At this time, you can actually switch between the two systems. Let's take a look at the detailed steps for switching between the two systems. How to switch between two systems in win10 system 1. Shortcut key switching 1. Press the "win" + "R" keys to open Run 2. Enter "msconfig" in the run box and click "OK" 3. In the open "System Configuration" In the interface, select the system you need and click "Set as Default". After completion, "Restart" can complete the switch. Method 2. Select switch when booting 1. When you have dual systems, a selection operation interface will appear when booting. You can use the keyboard " Up and down keys to select the system

How to switch between Apple dual systems when starting up Apple computers are powerful devices. In addition to their own macOS operating system, you can also choose to install other operating systems, such as Windows, to achieve dual system switching. So how do we switch between the two systems when booting? This article will introduce to you how to switch between dual systems on Apple computers. First of all, before installing dual systems, we need to confirm whether our Apple computer supports dual system switching. Generally speaking, Apple computers are based on

In Java, a carriage return is usually represented by a newline character. In Windows systems, "\r\n" is used to represent carriage return and line feed, while in Unix/Linux systems, "\n" is used. When reading text that contains carriage returns, Java treats these characters as normal characters. You can use the BufferedReader or Scanner classes to read text from an input stream until a carriage return or line feed is encountered.

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

In the application of excel software, we are accustomed to using shortcut keys to make some operations easier and faster. Sometimes there is related data between multiple tables in excel. When we view it, we have to constantly switch between tasks. If there is a faster switching method, it will save a lot of wasted time on switching, which will greatly help improve work efficiency. What method can be used to complete quick switching? To address this issue, the editor will talk about it today The content is: How to use the shortcut keys for switching workbooks in Excel. 1. First, you can see multiple workbooks at the bottom of the open excel table. You need to quickly switch between different workbooks, as shown in the figure below. 2. Then press the Ctrl key on the keyboard without moving, and select the job to the right if you need to

Win11 supports users to use the alt+tab shortcut key to bring up the desktop switching tool, but recently a friend encountered the problem that win11 alt+tab cannot switch the interface. I don’t know the reason or how to solve it. Why can't win11 alt+tab switch the interface? Answer: Because the shortcut key function is disabled, here is the solution: 1. First, we press "win+r" on the keyboard to open the run. 2. Then enter "regedit" and press Enter to open the group policy. 3. Then enter "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"
