C# WinForms - custom button unwanted border when form unselected

C#WinformsButtonBorder

C# Problem Overview


I'm having a problem with a custom button I have created in c# win forms.. The button appears fine when the form is selected but as soon as I click away from the form a border appears on the button. A good example of when this happens is when the desktop is clicked but the form is still maximised so you can see its contents. An image of the problem can be seen below:

Button border problem

This does not happen on all buttons, only when the button has been clicked prior (only appears on one button at a time). This lead me to believe that it was something to do with the button focus cues but these are set to false. The border is set to 0 and I also have tabstop set to false.

Any suggestions?

C# Solutions


Solution 1 - C#

When you're dealing with a custom button you should set:

button.TabStop = false;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;

Then since ButtonBase doesn't support the border color on Color.Transparent, you can overcome the issue by setting an Argb color:

button.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //transparent

Solution 2 - C#

fuex's answer can remove border in theory, but there is a bug that sometimes button will still have focus cue after you change the button enable status.

(I ran into this bug in .Net 4.0 and I don't know the bug is fixed or not in later versions).

To work around this bug, you should disable the ShowFocusCues property:

protected override bool ShowFocusCues => false; // return base.ShowFocusCues;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJpinView Question on Stackoverflow
Solution 1 - C#OmarView Answer on Stackoverflow
Solution 2 - C#FrankXView Answer on Stackoverflow