TabIndex changed to Enter for all forms in C#

人走茶凉 提交于 2019-12-13 21:13:31

问题


How can i change TabIndex from "tab" to "enter" for all forms in my win application. I know i can use event for every textbox like:

If (Keys.Enter Then) {
    SendKeys.Send("{TAB}")
}

but i don't want to do this 1000 times. Can i make this by default for all textboxes and forms?


回答1:


You can try to enable forms KeyPreview property. Then you'll be able to handle keystroke before controls get it

private void Form_KeyDown(object sender, KeyEventArgs e)
{
    if(Keys.Enter == e.KeyCode) 
    {
       SendKeys.Send("{TAB}");
       e.Handled = true;//set to false if you need that textbox gets enter key
    }
}



回答2:


You can bind all your textbox to the event "validation" (or something like that) and then call the same method which do the jump :)



来源:https://stackoverflow.com/questions/7254835/tabindex-changed-to-enter-for-all-forms-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!