Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the focus border of a CheckBox in C# Visual Studio?

Tried most properties and did not manage to completely disable the focus of a checkbox in visual studio. Does anyone know how to do it?

I am using a System.Windows.Forms.CheckBox object. I am using an image as background and when the CheckBox is in focused state a border is drawn, which makes the background image look pretty bad. So I want to get rid of it...

EDIT: Adding a picture to clarify the intention of this question...

enter image description here

The user can tap "TAB" and click on the object to see it displayed as focused. That was a problem for me since it made the GUI look simply terrible.

like image 407
Rodrigo Rutsatz Avatar asked Dec 20 '22 08:12

Rodrigo Rutsatz


1 Answers

The code for the CheckBox control that takes care of the painting is very elaborate, shared with Button and RadioButtion, supporting many styles. It cannot be overridden, the involved classes and methods are all internal.

But luckily you only want to mess with the focus rectangle. All you have to do is convince the control that it should not show it. That's very easy, add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox.

using System.Windows.Forms;

class MyCheckBox : CheckBox {
    protected override bool ShowFocusCues {
        get { return false; }
    }
}
like image 191
Hans Passant Avatar answered Jan 22 '23 07:01

Hans Passant