關於tabIndex的官方文章:
在一個容器里,使用Tab鍵會自動根據TabIndex大小順序聚焦下一個可以聚焦的控件。(不可以聚焦控件包括:不可見、禁用、TabStop為False)
如果一個該容器的控件已經聚焦完,繼續按Tab鍵,則會自動跳轉到下一個容器(按照容器的TabIndex順序)的控件聚焦。
Enter鍵實現Tab鍵一樣的效果,有兩種方法:窗體的SelectNextControl方法、發送Tab命令。
注意不應該使用控件的Enter事件來去實現,這個Enter命名有點迷惑性,因為通過鼠標點擊、以及自動聚焦都會觸發Enter事件。
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } /// <summary> /// 不可取的方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBox1_Enter(object sender, EventArgs e) { // SendKeys.Send("{Tab}"); } private void textBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { Control ctl; ctl = (Control)sender; this.SelectNextControl(ctl, true, true, true, true); } } private void textBox_KeyPress1(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { SendKeys.Send("{Tab}"); e.Handled = false; } } } }