How to intercept the TAB key press to prevent standard focus change in C#

后端 未结 5 767
伪装坚强ぢ
伪装坚强ぢ 2021-01-19 04:36

Normally when pressing the TAB key you change the focus to the next control in the given tab order. I would like to prevent that and have the TAB key do something else. In m

5条回答
  •  北海茫月
    2021-01-19 05:33

    You can try this code on your KeyDown event:

    if (e.KeyCode == Keys.Tab) {
      //your logic
      e.SuppressKeyPress = true;
    }
    

    If the button clicked is Tab, then do any custom logic you want, then call SuppressKeyPress to stop the KeyPress event from firing and invoking the normal Tab logic for you.

提交回复
热议问题