How to create a winform with buttons that will never attract keyboard focus
I have multiple text boxes on my winform. I have several buttons on it. Now when I type one text box and click the button, the input focus is lost from the text box and the button gets the keyboard focus. That is, the cursor is lost from the text field at the moment when we press the button. I don't want this behavior. I want the cursor to persist in my textbox even when I click the button. The real situation is that I have text and number buttons that can only be used from a touch screen.
source to share
In the event handler, click the Drawer button, explicitly set the focus to another control . Choose any control you think is reasonable for focusing after the button is pressed. For example, set focus to a TextBox using the following code:
textBox1.Focus();
This will prevent focus when the button is pressed.
Also, set the parameter TabStop
to false.
Other answers suggesting setting the property CanFocus
to false won't work because this property is read-only for buttons.
source to share