Winforms checkboxes background check Problem if text is not used in checkbox

I have several checkboxes in Winforms without the text property of all checkboxes, so the problem is that when I hover over the checkbox that was highlighted, but when I go to the checkbox using the tab key, it never stands out.

If anyone has a similar problem and has already been resolved, please help ..

+7
checkbox winforms focus
source share
4 answers

The problem is that when the checkbox receives focus, it only selects the text part of the control, which is empty in your case. You have several options:

1) For all your β€œempty” text fields, set the text property to a space. This will create a small selection when you go to the control.

2) Program the OnEnter and OnLeave events of this flag and draw / color a square around the entire control.

3) If you want the default behavior of MouseEnter to create a yellow highlight in the checkbox itself, create your own checkbox element 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); } } 
+7
source share

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 CheckBox.Padding as 2, 2, 0, 1 to get a border that was 1 pixel from the edge of the CheckBox .

+3
source share

You can fix this by setting the AutoSize = False property. When AutoSize is True, it acts like a shortcut with the AutoSize parameter set to true, since an empty label takes up almost no space on the screen. With AutoSize = False, you can manually set the bounding box for this flag.

0
source share

If you draw only a border, try setting these properties.

 AutoSize : False CheckAlign : MiddleCenter Font: Courier New, 12.25pt TextAlign: MiddleRight Padding : 0, 5, 0, 0 Size : 26, 26 Text : " " (two spaces) 
0
source share

All Articles