tags:

views:

94

answers:

2

Normally, when tabbing through controls on a form, when the focus is set to a CheckBox control, the text is outlined to show focus.

I am using a CheckBox without the text (so only the box is displayed). How can I force the focus outline to be drawn around the box instead of the text?

+1  A: 

I think you'd have to derive your own "textless checkbox" for the bounds of the control to be correctly recognized.

Might sound complicated but shouldn't be too difficult, I think. I created a few fancy checkboxes when I was learning .NET 1.x, though I don't recall being concerned about the focus rectangle.

Cerebrus
+3  A: 

Have a look - just to give you an idea:

public class MyCheckBox : CheckBox
{
  public MyCheckBox()
  {
    // AutoSize is virtual - so you should not call it here, just demo
    AutoSize = false;
    // You need padding to make the base.OnPaint() method leaving you some space
    Padding = new Padding( 2, 2, 0, 0 );
    Size = new Size( 17, 16 );
  }

  protected override void OnPaint( PaintEventArgs pevent )
  {
    base.OnPaint( pevent );
    if( !Focused )
    {
      return;
    }
    using( var pen = new Pen( Color.Black ) )
    {
      pen.DashStyle = DashStyle.Dot;
      pevent.Graphics.DrawRectangle( pen, new Rectangle( 0, 0, 16, 15 ) );
    }
  }
}
tanascius
+1 for providing the code! ;-)
Cerebrus