8176135
8176135

Reputation: 4133

Enter focus on a button while still being able to type in a textbox

Is there a way to have both a textbox and a button in focus? I know that I can use textbox1.focus to focus the textbox, and same with the button, but is there a way to be able to have enter focus on the button, while still typing in the textbox?

I know that I can just use the key-press event to capture the enter key, but I'm wondering if I can have enter focus on the button.

Just curious to know if this is possible, a simple yes or no is all that's needed. Thanks!

Upvotes: 1

Views: 765

Answers (3)

Leandro
Leandro

Reputation: 1555

You don't need to have focus on both controls to get the behavior (I assume) you want. Just set the parent form's or dialog's AcceptButton property to the button you are talking about; its Click event should be triggered when you press the ENTER key even while the TextBox has focus.

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton.aspx

To make the answer complete (edit):

As stated above in my comment to your question (and likewise by other contributors): only one control can have focus at any given time. So, the answer to your original question is:
No, there is no way.

Upvotes: 1

Tobias Knauss
Tobias Knauss

Reputation: 3539

As Bradley said, you can't.
But for your aim, there is a shortcut, called Form.keypreview property, which let's you evaluate keys before they reach the textbox or other controls.

Upvotes: 1

BradleyDotNET
BradleyDotNET

Reputation: 61379

Only one element can have input focus at a time. You can't specify the focus for just one key.

So basically, no, you can't do that.

Upvotes: 4

Related Questions