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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2212237/
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-07 00:28:55  来源:igfitidea点击:

C#: How to make pressing enter in a text box trigger a button, yet still allow shortcuts such as "Ctrl+A" to get through?

c#textboxsubmitkeydownenter

提问by Jonathan Chan

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.

ShortcutsEnabled 设置为 true。

Thanks in advance!

提前致谢!

采纳答案by Rob Cooper

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

不能AcceptButton用于窗体属性窗口吗?这将设置Enter按键的默认行为,但您仍然可以使用其他快捷键。

回答by Hadron

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.

如果您希望返回仅在用户位于文本框中时触发操作,您可以将所需的按钮分配给 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
    }

回答by Mawardy

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

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

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

hope it works

希望它有效

回答by Guss Davey

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.

如果这样做是 ASP.NET,则不需要任何客户端代码。下面的例子是一个带有搜索按钮的 boostrap 输入框,带有一个字体很棒的图标。

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.

您将看到,我使用了 asp:Panel,而不是使用带有“input-group”类的常规 <div> 标记。DefaultButton 属性设置为我的按钮的 id,可以解决问题。

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

在下面的示例中,在输入文本框中输入内容后,您只需按 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>