Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in button (.Net 2008, Framework 3.5)

When a button is focused by pressing Tab key, a rectangle appears on it. Even if the button's TabStop property is set to false, when the button is clicked with mouse the rectangle appears. Is it possible to stop the rectangle from appearing? Please help. Regards.

like image 775
priyamtheone Avatar asked May 04 '26 05:05

priyamtheone


1 Answers

That rectangle that is appearing on your button is called a "focus rectangle." It indicates which control on the form currently has the input focus.

The explanation for the problem that you're running into is that, even when the button is not a tab stop, it still becomes selected when it is clicked on with the mouse, and therefore the focus rectangle still appears. The TabStop property only governs whether or not the control can receive focus with the Tab key, not whether it is selectable by the user.

The focus rectangle is useful to indicate to the user which control has the focus. Pressing the Enter or Space keys with the button selected would cause the button to get "pushed." Without the focus rectangle, it can be difficult for keyboard users to navigate your application.

If you simply want to prevent the button from getting the focus at all (and thus prevent the focus rectangle from appearing), you can set its Enabled property to False. Of course, this will also prevent the user from clicking on the button.

If you want the button to remain clickable but prevent drawing a rectangle when it has the focus (at the expense of your program's usability, I might caution), you will have to create your own custom control that derives from the existing Button control. In your control, you can override the ShowFocusCues property (which is True by default on a Button control) to return False. For example:

public class NoFocusButton : Button
{
    protected override bool ShowFocusCues
    {
        get
        {
                return false;
        }
    }
}

Alternatively, you can override the OnPaint event in your derived button control. This will allow you complete control over the way that your button is drawn (including removing the focus rectangle), which comes with just as many risks as rewards. See this answer to another question for more information on this route.

like image 70
Cody Gray Avatar answered May 05 '26 19:05

Cody Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!