How to do this? I read that it is done in event.

Dani AI

Generated

Quick clarification: the right fix depends on the platform. For Windows Forms the built‑in solution is to set the form’s AcceptButton (as suggested) — that makes Enter trigger the chosen button everywhere on the form (and ’s follow‑up shows it’s usually the simplest). If you want more selective behavior you can attach a key handler like and suggested. Because this thread is tagged "javascript", below is the idiomatic web approach plus a few troubleshooting notes you can apply immediately.

/* HTML: make sure the input and button exist (button may be type="button" or "submit") */
const input = document.getElementById('myInput');
const button = document.getElementById('myButton');

input.addEventListener('keydown', function (e) {
  if (e.key === 'Enter') {
    e.preventDefault();                // optional: stop native form submit if handling in JS
    if (!button.disabled) button.click();
  }
});

Troubleshooting and tips:

  • If the control is inside a <form>, a single‑line <input> will usually submit the form automatically. Use a proper <button type="submit"> and handle the form’s submit event when possible (better for accessibility).
  • For multi‑line controls (textarea or WinForms multiline textbox) Enter normally inserts a newline. Use Ctrl+Enter or Shift+Enter for submit, or detect e.ctrlKey && e.key === 'Enter'.
  • Prefer keydown + e.key === 'Enter' over deprecated keyCode. Attach handlers after DOM load.
  • If nothing fires, check that the button isn’t disabled and that no other handler calls e.stopPropagation() or e.preventDefault() earlier.
  • For WinForms where Enter should be ignored in one textbox, set AcceptButton to null or handle the textbox key event and set e.Handled appropriately.

Use the platform-native mechanism (Form.AcceptButton or HTML form submit) where possible; event handlers are for custom behaviors.

Recommended Answers

All 4 Replies

Do you want it so that if you press enter in your textbox it will click a button for you?

If so you can use the following:

private void txtBox_keyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) //13 = enter
            {
                button1.PerformClick();
            }
        }

If this is not what you want you should clarify your question.

Alternativley, set the forms AcceptButton property to the button you want to execute on Enter.
Setting the forms CancelButton property will do the same thing for Escape.
And setting the forms HelpButton captures the F1 key.

commented: i used this. thanks +1

Your can use this code:

  private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            String key = Convert.ToString(e.KeyChar);
            if (key == "r")//    r notifies return key
            {
                btnSearch_Click(null, null);
            }
        }

I used the acceptbutton property. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.