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

5

29

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!

Tellurion answered 6/2, 2010 at 5:59 Comment(2)
No repro, I didn't expect any. You'll need e.SuppressKey = true to stop the beeping.Meakem
As for the title: you could have put the yet-part in the first sentence of your question body. The first sentence now is off-topic anyway.Inspector
O
41

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.

Owenism answered 6/2, 2010 at 7:0 Comment(3)
I see! Thanks for clarifying this. I'm obviously new to C#, so simple things like this confuse me. Thanks!Tellurion
what if you want multiple controls to each accept enter, though, and not necessarily with a button for each?Konstantin
Nyerguds, read the answer below. Basically, setup triggers when each control gets focus to set the correct AcceptButton. I have all my "Leave" functions set the AcceptButton back to the OKbtn, not the null as the answer below suggests, but same same.Hanyang
O
16

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
    }
Opulent answered 16/12, 2013 at 18:4 Comment(1)
In case of a UserControl, you can use ParentForm.AcceptButtonGauss
C
14

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

Cilka answered 17/12, 2014 at 21:22 Comment(2)
Can that be expanded for the event listeners of other controls than buttons? I can't seem to find any ListBox.PerformDoubleClick() to trigger its double-click action.Konstantin
Ah, nevermind. Easier to just call the underlying listener function. By the way... this code doesn't seem to work if the form has an AcceptButton set.Konstantin
L
1

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>
Loader answered 2/4, 2017 at 7:49 Comment(0)
C
0

https://mcmap.net/q/149980/-stop-the-39-ding-39-when-pressing-enter

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

Chevrotain answered 25/8, 2020 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.