C# 如何以编程方式在控件之间制表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1179412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:09:53  来源:igfitidea点击:

C# How to programmatically tab between controls

c#

提问by NXT

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.

我知道 Focus(),但我宁愿做一些自动遵守 Tab 键顺序的事情。通过模拟键盘导航,我不必担心为每种类型的控件重新发明复杂的行为。

Does anyone know how to do this?

有谁知道如何做到这一点?

Thanks!

谢谢!

采纳答案by Thomas

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

您可以使用 P/Invoke 调用 Windows API 函数keybd_event来模拟按 Tab 键。

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

奖励:您也可以使用您的设备在文本编辑器中输入标签!;)

回答by Randolpho

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

对于 Winforms,你想要Control.GetNextControl()方法

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

对于 WPF,您需要UIElement.MoveFocus()方法

回答by Will Eddins

In Winforms:

在 Winforms 中:

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

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

要模拟 Tab 按下,我相信它如下:

SendKeys.Send("{TAB}");