Search code examples
c#buttoncomponentsstyles

C# Button Background Color


i have a Windows Form, and i want make a modern design.
I'm just having a problem with button styling, you can help-me?

I want to remove or hide the background color when the button was clicked, I managed to remove the background color from when the mouse is over the component with the following code:

FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;

Now i need remove or hide this (grey background):
Form Picture

How i make this?

Thank you!


Solution

  • I obtained the solution with many attempts.
    Just change the FlatAppearance.MouseOverBackColor and FlatAppearance.MouseDownBackColor to the background color of the Form or the item it is attached to.

    Example: You have a form with a panel, the color of the panel is red, so set the attributes to red, or just put "Color.Transparent"

    buttonName.FlatAppearance.MouseOverBackColor = Color.FromArg(255, 0, 0) // red;
    buttonName.FlatAppearance.MouseDownBackColor = Color.Transparent; // same result
    

    So, if you have a class that extends the Button object, just make code like this:

    public class MyButton : Button
    {
            public MyButton()
            {
                FlatAppearance.MouseOverBackColor = Color.Transparent; // or Color.[Preference]
                FlatAppearance.MouseDownBackColor = Color.Transparent; // or Color.[Preference]
            }
    
            // Rest of your code...
    }
    

    Otherwise, do a foreach, as below

    foreach (var button in this.Controls.OfType<Button>())
    {
             button.FlatAppearance.MouseOverBackColor = Color.Transparent; // or Color.[Preference]
             button.FlatAppearance.MouseDownBackColor = Color.Transparent; // or Color.[Preference]
    }
    

    The codes above are examples for everyone to be able to build their own logic, if in doubt I can help you.