C#:如何使在文本框中按 Enter 键触发按钮,但仍允许使用“Ctrl”A 等快捷键要通过?

发布于 2024-08-20 10:46:29 字数 401 浏览 6 评论 0原文

抱歉,标题很长,但我想不出其他方式来表达它。

我有这个:

    private void textBoxToSubmit_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            buttonSubmit_Click((object)sender, (EventArgs)e);
        }
    }

...为了使在文本框中按 Enter 触发“提交”按钮。然而,这也阻止了快捷方式的通过。 (不太确定它与此有什么关系,也许只是多键组合?)

ShortcutsEnabled 设置为 true。

提前致谢!

Sorry for the long title, but I couldn't think of another way to put it.

I have this:

    private void textBoxToSubmit_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            buttonSubmit_Click((object)sender, (EventArgs)e);
        }
    }

... in order to make pressing enter in the text box trigger the "submit" button. However, this also prevents shortcuts from going through. (not quite sure what it has to do with that, maybe only multi-key combos?)

ShortcutsEnabled is set to true.

Thanks in advance!

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

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

发布评论

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

评论(5

烂柯人 2024-08-27 10:46:29

您不能在“表单属性”窗口中使用 AcceptButton 吗?这设置了 Enter 按键的默认行为,但您仍然可以使用其他快捷键。

Can you not use AcceptButton in for the Forms Properties Window? This sets the default behaviour for the Enter key press, but you are still able to use other shortcuts.

屋檐 2024-08-27 10:46:29

如果您希望返回仅在用户位于文本框中时触发操作,则可以将所需的按钮分配给 AcceptButton 控件,如下所示。

    private void textBox_Enter(object sender, EventArgs e)
    {
        ActiveForm.AcceptButton = Button1; // Button1 will be 'clicked' when user presses return
    }

    private void textBox_Leave(object sender, EventArgs e)
    {
        ActiveForm.AcceptButton = null; // remove "return" button behavior
    }

If you want the return to trigger an action only when the user is in the textbox, you can assign the desired button the AcceptButton control, like this.

    private void textBox_Enter(object sender, EventArgs e)
    {
        ActiveForm.AcceptButton = Button1; // Button1 will be 'clicked' when user presses return
    }

    private void textBox_Leave(object sender, EventArgs e)
    {
        ActiveForm.AcceptButton = null; // remove "return" button behavior
    }
万水千山粽是情ミ 2024-08-27 10:46:29

您可以使用 KeyPress 而不是 KeyUp 或 KeyDown 它更有效
这是如何处理

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            e.Handled = true;
            button1.PerformClick();
        }
    }

希望它有效的方法

You can Use KeyPress instead of KeyUp or KeyDown its more efficient
and here's how to handle

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            e.Handled = true;
            button1.PerformClick();
        }
    }

hope it works

年华零落成诗 2024-08-27 10:46:29

如果是 ASP.NET,则不需要任何客户端代码。
下面的示例是一个带有搜索按钮和 fontawesome 图标的 boostrap 输入框。

您将看到它代替了常规的 <分区>带有“输入组”类的标签我使用了 asp:Panel。将 DefaultButton 属性设置为我的按钮的 id 就可以了。

在下面的示例中,在输入文本框中输入内容后,只需按 Enter 键即可提交。

<asp:Panel DefaultButton="btnblogsearch" runat="server" CssClass="input-group blogsearch">
<asp:TextBox ID="txtSearchWords" CssClass="form-control" runat="server" Width="100%" Placeholder="Search for..."></asp:TextBox>
<span class="input-group-btn">
    <asp:LinkButton ID="btnblogsearch" runat="server" CssClass="btn btn-default"><i class="fa fa-search"></i></asp:LinkButton>
</span></asp:Panel>

You do not need any client side code if doing this is ASP.NET.
The example below is a boostrap input box with a search button with an fontawesome icon.

You will see that in place of using a regular < div > tag with a class of "input-group" I have used a asp:Panel. The DefaultButton property set to the id of my button, does the trick.

In example below, after typing something in the input textbox, you just hit enter and that will result in a submit.

<asp:Panel DefaultButton="btnblogsearch" runat="server" CssClass="input-group blogsearch">
<asp:TextBox ID="txtSearchWords" CssClass="form-control" runat="server" Width="100%" Placeholder="Search for..."></asp:TextBox>
<span class="input-group-btn">
    <asp:LinkButton ID="btnblogsearch" runat="server" CssClass="btn btn-default"><i class="fa fa-search"></i></asp:LinkButton>
</span></asp:Panel>
一瞬间的火花 2024-08-27 10:46:29

https://stackoverflow.com/a/16350929/11860907

如果添加e.SuppressKeyPress = true;< /code> 如此链接中的答案所示,您将抑制出现的烦人的叮当声。

https://stackoverflow.com/a/16350929/11860907

If you add e.SuppressKeyPress = true; as shown in the answer in this link you will suppress the annoying ding sound that occurs.

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