I have the following method which works fine:
button21.FlatAppearance.BorderSize = 0;
button21.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
As you can see, I have many buttons (~30), so instead of applying style on every button – why not apply on all of them at once on Form_Load event? So I wrote this method:
public void ChangeButtonStyles()
{
foreach (Control con in this.Controls)
{
if (con is Button)
{
Button but = con as Button;
but.FlatAppearance.BorderSize = 0;
but.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
}
}
}
But it’s not working because when you hover a mouse on a button – you can see background change, which proves that but.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent; is not working
Why isn’t the code working?
public Form1()
{
InitializeComponent();
// works
//button21.FlatAppearance.BorderSize = 0;
//button21.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
// doesn't work
ChangeButtonStyles();
}
EDIT: Solution
public void ChangeButtonStyles()
{
foreach (Control con in this.Controls)
{
if (con is Button)
{
Button but = con as Button;
but.FlatAppearance.BorderSize = 0;
but.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
}
if (con is GroupBox)
{
foreach (Control subcon in con.Controls)
{
if (subcon is Button)
{
Button but = subcon as Button;
but.FlatAppearance.BorderSize = 0;
but.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
}
}
}
}
}
Have you tried debugging the code to see if it actually gets executed? this.Controls will return the top level controls. If the buttons are nested inside other controls you need to be more recursive.