C# - Add Mousehover to each Button in a Flowlayout

Asked

Viewed 29 times

1

How do I add a Mousehover event to each existing button in a Flowlayoutpanel?

I want that when you hover the mouse on each button in this Panel the mouse pointer changes to "Hand" and that the background color of that button is a lighter blue

Code I tried to make:

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 ?;
    }

1 answer

1


You have to first go through the controls of FlowLayoutPanel but only the kind Button, and then associate the event using the sender of the 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;
}
  • 1

    It worked! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.