C# 禁用TAB键

发布于 2024-08-05 04:09:28 字数 476 浏览 5 评论 0原文

我有这段代码:

this.searchInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.inputKeypress);

private void Keypress(object sender, KeyPressEventArgs e)
{
    // If Tab has been pressed
    if(122 == (int)e.KeyChar)
    {
        switchTab(sTab);
        MessageBox.Show(sTab);
    }
}

它的作用是将焦点设置到另一个元素。 但是,当焦点设置到 TextBox 时,我按 TAB 键,它只会在 TextBox 中创建一个选项卡,并且不会将焦点设置在下一个元素上。

有人知道我怎样才能完成这项工作吗?

我尝试设置 e.Handled = true;但这没有用...

I have this code:

this.searchInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.inputKeypress);

private void Keypress(object sender, KeyPressEventArgs e)
{
    // If Tab has been pressed
    if(122 == (int)e.KeyChar)
    {
        switchTab(sTab);
        MessageBox.Show(sTab);
    }
}

What it does is that it sets focus to another element.
But, when the focus is set to a TextBox, and I press TAB, it just makes a tab in the TextBox, and does not set focus on the next element.

Anyone got an idea how can I make this work?

I've tried to set e.Handled = true; but that didn't work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

回眸一笑 2024-08-12 04:09:28

您是否尝试过将 TextBox 上的 AcceptsTab 设置为 false

编辑:

是的。它不起作用。奇怪......它仍然在文本框中列表

这毫无意义。我运行了一个小型测试应用程序,当 AcceptsTab Multiline 时,Tab 键只会将焦点从 TextBox 上移开。 > 属性均为 true,无论为 KeyPress 定义什么事件处理程序。

您确定其他代码没有将 AcceptsTab 设置为 true 吗?如果是,将 Multiline 设置为 false 是否会完全改变选项卡行为?您可以发布更多相关代码吗?

Have you tried setting AcceptsTab on the TextBox to false?

Edit:

yep. It does not work. Strange... It still tabulates in the textbox

That makes little sense. I ran a small test app, and the tab key only brings focus away from the TextBox when its AcceptsTab and Multiline properties are both true, regardless of an event handler being defined for KeyPress.

Are you sure some other code isn't setting AcceptsTab to true? If you are, does setting Multiline to false change the tab behaviour at all? Could you post more of your relevant code?

余生共白头 2024-08-12 04:09:28

将文本框的 AcceptsTab 属性设置为 false

Set the AcceptsTab property of the text box to false?

我最亲爱的 2024-08-12 04:09:28

您需要像这样创建一个控件实例,并重写以下方法:

using System.Windows.Forms

//optional namespace

public class NoTabTextBox : TextBox
{
    protected override bool IsInputKey(Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Tab:
                return true;
        }
        return base.IsInputKey(keyData);
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab) { e.Handled = true; e.SuppressKeyPress = true; }
        base.OnKeyDown(e);
    }
}

构建解决方案,然后通过在工具箱中的用户控件下找到它,用新的“NoTabTextBox”替换您的常规 TextBox。

这将捕获 Tab 键并强制它不执行任何操作。

You'll need to create an instance of the control like so, and override the following methods:

using System.Windows.Forms

//optional namespace

public class NoTabTextBox : TextBox
{
    protected override bool IsInputKey(Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Tab:
                return true;
        }
        return base.IsInputKey(keyData);
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab) { e.Handled = true; e.SuppressKeyPress = true; }
        base.OnKeyDown(e);
    }
}

Build the solution, then subsitute you're regular TextBox with the new one 'NoTabTextBox', by finding it under the user controls in toolbox.

This will capture the Tab key and force it to do nothing.

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