Reputation: 537
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.
Upvotes: 3
Views: 234
Reputation: 537
class CustomButton : System.Windows.Forms.Button { private bool _DisplayFocusCues = true; protected override bool ShowFocusCues { get { return _DisplayFocusCues; } } public bool DisplayFocusCues { get { return _DisplayFocusCues; } set { _DisplayFocusCues = value; } } }
Using this class you can set DisplayFocusCues at design time too.
Upvotes: 0
Reputation: 244712
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.
Upvotes: 7