Reputation: 4133
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
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
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
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
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