Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winforms Checkbox Focus Problem if no Text is Applied on Checkbox

I have a multiple checkboxes on a Winforms without having the Text Property of all checkboxes, so the problem is that when i hover a mouse on the checkbox it highlighted but when i go to the checkbox using tab key it never get highlighted..

If anyone have the similar issue and already solved it Please help..

like image 660
Sameep Avatar asked Jan 07 '10 10:01

Sameep


2 Answers

The problem is that when a check box gets focus it highlights only the text portion of the control which is empty in your case. You have a few choices:

1) For all your "blank" text boxes, set the text property to a space. This will create a small highlighted portion when you tab to the control.

2) Program the OnEnter and OnLeave events of the checkbox, and draw/paint a square around the whole control.

3) If you want the default MouseEnter behaviour which creates a yellowish highlight on the check box itself, create your own check box control as follows:

public class MyCB : CheckBox
{
    protected override void OnEnter(EventArgs e)
    {
      base.OnEnter(e);
      base.OnMouseEnter(e);
    }

    protected override void OnLeave(EventArgs e)
    {
      base.OnLeave(e);
      base.OnMouseLeave(e);
    }
}
like image 109
AKoran Avatar answered Sep 21 '22 23:09

AKoran


I added an event handler for the CheckBox.Paint event and added the following:

private void checkBox1_Paint(object sender, PaintEventArgs e)
{
    CheckBox checkBox = sender as CheckBox;

    if (checkBox.Focused)
    {
        // e.ClipRectangle is affected by checkBox.Padding. Be careful when changing the Padding.
        ControlPaint.DrawFocusRectangle(e.Graphics, e.ClipRectangle, checkBox.ForeColor, checkBox.BackColor);
    }
}

I also adjusted the CheckBox.Padding to be 2, 2, 0, 1 in order to get a border that was 1 pixel from the edge of the CheckBox.

like image 40
Ecyrb Avatar answered Sep 21 '22 23:09

Ecyrb