C # - Add MouseHover to each Button in a FlowLayout

1

How do I add a MouseHover event to every existing button in a FlowLayoutPanel?

I want the mouse pointer to change to "Hand" when the mouse pointer is flipped on each button in this Panel and the background color of this button is a lighter blue

Code I tried to do:

private void frmMain_Load(object sender, EventArgs e)
    {
        foreach (Button bt in panelBotoes.Controls)
        {                
            bt.MouseHover += new EventHandler(focarBotao(bt));                
        }
    }

    private EventHandler focarBotao(Button bt)
    {
        bt.BackColor = Color.LightBlue;
        Cursor.Current = Cursors.Hand;
        return ?;
    }
    
asked by anonymous 28.05.2018 / 11:09

1 answer

1

You have to first walk through the controls of FlowLayoutPanel but only those of type Button , and then associate the event using% method%:

private void frmMain_Load(object sender, EventArgs e)
{
    foreach (Button b in panelBotoes.Controls.OfType<Button>())
    {
        b.MouseHover += B_MouseHover;
    }
}

private void B_MouseHover(object sender, EventArgs e)
{
    ((Button)sender).BackColor = Color.LightBlue;
    ((Button)sender).Cursor = Cursors.Hand;
}
    
28.05.2018 / 13:08