简体   繁体   中英

Set focus to another Control

我想在C#Windows应用程序(C#2005)中在textbox1中按ENTER键时将焦点从一个textbox1设置到另一个textbox2

处理textbox1KeyPressKeyDown事件,然后调用textbox2.Focus()

First, you will have to set the KeyPreview property of the Form set to true . Then you will have to override the form's OnKeyDown method and make a case like:

if(e.KeyCode == Keys.Enter)
{
      Control ctlNext = this.GetNextControl(this.ActiveControl, true);
      ctlNext.Focus();
}
else
{
      base.OnKeyDown(e);
}

Mind you that this code will work for every control on the form, and move the focus to the next one. If you just want this code to work for the textboxes you could add a check like:

if(this.ActiveControl is TextBox)
{
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM