losingsleeep
losingsleeep

Reputation: 1879

How to remove focus from a single control?

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?

Upvotes: 5

Views: 35749

Answers (2)

Javed Akram
Javed Akram

Reputation: 15344

Use TabStop property of button

button1.TabStop = false;

Upvotes: 4

Unmesh Kondolikar
Unmesh Kondolikar

Reputation: 9312

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.

Upvotes: 10

Related Questions