Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Winforms Checkbox not indicating focus

If a checkbox is the first control in the tab order (0) it does not indicate it has focus when the form is displayed. It does, indeed, have focus which you can demonstrate by hitting the spacebar to check/uncheck the control. If you tab then shift-tab to return to the checkbox, the label is outlined to indicate focus. Easily testable by creating a new form with a checkbox, textbox, and button controls. Tab order set to that order. Launch form. There will be no indication that the checkbox has focus. Tab though controls and when you return to the textbox, it will be outlined. I have tried programmatically setting focus using Control.Focus() and Control.Select() in the form Load and Shown methods.

How does one make the checkbox appear to have focus when the form is launched?

like image 771
Mark Clark Avatar asked Jul 21 '16 12:07

Mark Clark


1 Answers

This is standard behavior. As you've pointed out, since the control is the first one in the tab order and can take the focus, it is actually focused when the form is first displayed.

The problem is that, since Windows 2000, the focus rectangles and underlined mnemonics have been hidden by default. They do not show up until you start interacting with the program using the keyboard interface. Simply pressing the Alt key is enough to make them appear. So is actually using Tab and Shift+Tab.

The idea is to reduce visual noise in Windows, namely focus indicators and access key underlines in menus and windows. Aesthetically, these things are distracting and intimidating. Functionally, they're only useful when you're navigating by keyboard. They don't add significant value when you're just using the mouse. In fact, they're often redundant.

Why now? Every good thing must start somewhere. Windows will look cleaner and simpler.

[…]

Of course, the keyboard indicators will come back when there is any demonstration of keyboard navigation by the user. The indicators will appear and disappear appropriately. Finally, if you don't like the behavior at all, you can disable it from the Display control panel.

So you are doing nothing wrong. No programmatic usage of Select or Focus or equivalent is going to make that focus rectangle show up. The control is already focused.

There are only two things you could possibly do:

  1. Change the system setting so that focus indicators are always shown. Since this is a global setting, it should only be manipulated by the end-user, not by your application. Therefore, there is no need to call an API function to do it, just visit the control panel.

  2. Override the control's drawing behavior and force it to draw the focus indicator, regardless of what the system thinks it should do. You could do this by completely owner-drawing the control, but it's probably easier just to use this control class instead:

    public class ForcedFocusCheckBox : CheckBox
    {
        protected override bool ShowFocusCues
        {
            get  { return true; }
        }
    }
    

But honestly, you shouldn't do either of these things. This is a standard platform convention. The same thing happens for all of the built-in dialogs. If a user doesn't already know it, he or she probably doesn't do much keyboard interaction, and they won't be expecting to press the spacebar to toggle your checkbox in the first place. They'll just use the mouse.

like image 57
Cody Gray Avatar answered Oct 09 '22 03:10

Cody Gray