C# 中如何检测 Tab 键按下?
我想检测何时在文本框中按下 Tab 键并聚焦面板中的下一个文本框。
我已经尝试过 keyPressed 方法和 keyDown 方法。但是当我运行程序并调试时,按下 Tab 键时不会调用这些方法。 这是我的代码。
private void textBoxName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
textBoxUsername.Focus();
}
}
private void textBoxName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar==(char)Keys.Tab)
{
textBoxUsername.Focus();
}
}
请纠正我。谢谢。
I want to detect when tab key is pressed in a textBox and focus the next textbox in the panel.
I have tried out keyPressed method and keyDown method. But when I run the program and debug those methods are not calling when the tab key is pressed.
Here is my code.
private void textBoxName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
textBoxUsername.Focus();
}
}
private void textBoxName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar==(char)Keys.Tab)
{
textBoxUsername.Focus();
}
}
Please correct me.Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为什么你需要这种复杂的功能呢? WinForms 自动为您完成此操作。您只需设置正确的 Tab 键顺序即可。
Why do you need that complication at all? WinForms does it for you automatically. You just need to set the correct tab order.
转到文本框的属性并指定正确的 tabindex 顺序
go to the properties of text box and assign correct order of tabindex
您应该使用 tabOrder 代替。
You should use tabOrder instead.
您想要“离开”事件。我只是将其放入默认的 C# WinForms 应用程序中:
它按照您的预期工作。显然,您可以在
Leave()
处理程序中做任何您想做的事情,包括将焦点强制到其他地方,但要小心不要让用户感到困惑......You want the "leave" event. I just threw this into the default C# WinForms application:
It works as you would expect. Obviously you can do anything you want in the
Leave()
handler, including forcing the focus elsewhere, but be careful not to confuse the user...您可以尝试重写 ProcessCmdKey 方法,如下所示< /a>
You can try overriding the ProcessCmdKey method like this
如果按下 TAB 键时 textBoxName 具有焦点,则仅触发“KeyDown”事件。
您只需设置正确的 Tab 键顺序即可。
If textBoxName has a focus while pressing the TAB Key , then only the "KeyDown" event triggers.
You just need to set the correct tab order.
如果您正在处理面板内的文本框,设置正确的选项卡索引应该可以完美完成工作。但是,如果您正在处理其他面板中的其他文本框,请说:
panel1 有 textbox1
panel2 有 textbox2
panel3 有 textbox3
以下是您需要执行的操作:
设置
TabStop = False
属性
所有文本框。默认情况下,此设置为 True。为每个面板设置正确的
TabIndex
,例如面板1 TabIndex = 0;
面板2 TabIndex = 1;
panel3 TabIndex = 2;
然后尝试此代码
private void textBox1_KeyDown(对象发送者,KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Tab))
this.textBox3.Focus();
}
If you are dealing with the text boxes inside a Panel, setting the correct tab index should do the job perfectly. But, if you are dealing with other text box from other Panel say:
panel1 has textbox1
panel2 has textbox2
panel3 has textbox3
Here's what you need to do:
Set the
TabStop = False
property
of all the textboxes. By default, this is set to True.Set the correct
TabIndex
for each panel, e.g.panel1 TabIndex = 0;
panel2 TabIndex = 1;
panel3 TabIndex = 2;
Then try this code
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Tab))
this.textBox3.Focus();
}