C# 中如何检测 Tab 键按下?

发布于 2024-09-04 01:01:53 字数 463 浏览 7 评论 0原文

我想检测何时在文本框中按下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烟若柳尘 2024-09-11 01:01:53

为什么你需要这种复杂的功能呢? 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.

舂唻埖巳落 2024-09-11 01:01:53

转到文本框的属性并指定正确的 tabindex 顺序

go to the properties of text box and assign correct order of tabindex

在梵高的星空下 2024-09-11 01:01:53

您应该使用 tabOrder 代替。

You should use tabOrder instead.

酒绊 2024-09-11 01:01:53

您想要“离开”事件。我只是将其放入默认的 C# WinForms 应用程序中:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        /* 
              ... misc housekeeping ... 
        */

        private void OnLeave(object sender, EventArgs e)
        {
            lblMsg.Text = "left field 1";
        }

        private void OnLeave2(object sender, EventArgs e)
        {
            lblMsg.Text = "left field 2";
        }
    }
}

它按照您的预期工作。显然,您可以在 Leave() 处理程序中做任何您想做的事情,包括将焦点强制到其他地方,但要小心不要让用户感到困惑......

You want the "leave" event. I just threw this into the default C# WinForms application:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        /* 
              ... misc housekeeping ... 
        */

        private void OnLeave(object sender, EventArgs e)
        {
            lblMsg.Text = "left field 1";
        }

        private void OnLeave2(object sender, EventArgs e)
        {
            lblMsg.Text = "left field 2";
        }
    }
}

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...

慕烟庭风 2024-09-11 01:01:53

如果按下 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.

携余温的黄昏 2024-09-11 01:01:53

如果您正在处理面板内的文本框,设置正确的选项卡索引应该可以完美完成工作。但是,如果您正在处理其他面板中的其他文本框,请说:

panel1 有 textbox1

panel2 有 textbox2

panel3 有 textbox3

以下是您需要执行的操作:

  1. 设置 TabStop = False 属性 所有文本框。默认情况下,此设置为 True。

  2. 为每个面板设置正确的TabIndex,例如

    面板1 TabIndex = 0;
    面板2 TabIndex = 1;
    panel3 TabIndex = 2;

  3. 然后尝试此代码

    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:

  1. Set the TabStop = False property of all the textboxes. By default, this is set to True.

  2. Set the correct TabIndex for each panel, e.g.

    panel1 TabIndex = 0;
    panel2 TabIndex = 1;
    panel3 TabIndex = 2;

  3. Then try this code

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode.Equals(Keys.Tab))
    this.textBox3.Focus();
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文