Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove focus from a single control?

Tags:

c#

.net

winforms

In my C# WinForms program, I have a form that has only a single Button control on it. By default, that Button control receives the focus on the form. But I don't want the Button to ever get focus.

Is there a solution, even one that would require a call to an unmanaged API?

like image 489
losingsleeep Avatar asked Dec 28 '22 02:12

losingsleeep


2 Answers

In your form Load event you can set the focus on some other control.

If you do not want the control to ever get focus via keyboard, you can also set its TabStop property to false.

If you want that the button should not have focus when you open the form, then you need to correct the TabIndex property. The TabIndex property has an integer as value which specifies the order in which the controls get focus when tab key is pressed. If the control has TabIndex set to 0 then change it to some other value.

Check the documentation for TabIndex and TabStop properties on MSDN.

like image 55
Unmesh Kondolikar Avatar answered Jan 10 '23 03:01

Unmesh Kondolikar


Use TabStop property of button

button1.TabStop = false;
like image 26
Javed Akram Avatar answered Jan 10 '23 04:01

Javed Akram