简体   繁体   中英

C# How to programmatically tab between controls

I would like to be able to programmatically emulate the keyboard navigation for dialog boxes.

I have a custom hardware device with a keypad that I would like to use for dialog box navigation.

I know about Focus(), but I'd rather do something that automatically respected the tab order. By emulating the keyboard navigation I don't have to worry about re-inventing complex behavior for each type of control.

Does anyone know how to do this?

Thanks!

For Winforms, you want want the Control.GetNextControl() method

For WPF, you want the UIElement.MoveFocus() method

In Winforms:

Control nextControl = this.GetNextControl(myControl, true);

To simulate a tab press, I believe it's the following:

SendKeys.Send("{TAB}");

You could use P/Invoke to call the Windows API function keybd_event to simulate pressing the Tab key.

Bonus: you can use your device to enter tabs into a text editor as well! ;)

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