Home > Mobile >  How can I fire "MouseEnter" of a control when I do "MouseEnter" to another?
How can I fire "MouseEnter" of a control when I do "MouseEnter" to another?

Time:09-14

Let's say I have a panel and inside this panel I have a label. What I want to do is to fire panel's MouseEnter but when I do MouseEnter on label control. Any idea?

CodePudding user response:

When MouseEnter event of your panel is fired all handlers that are subscribed to this event will be called. So, instead of firing panel's event you can call those handlers. See following demo code.

Color backColor;

private void Panel1_MouseEnter(object sender, EventArgs e)
{
    backColor = panel1.BackColor;
    panel1.BackColor = Color.Red;
}

private void Panel1_MouseLeave(object sender, EventArgs e)
{
    panel1.BackColor = backColor;
}

private void Label1_MouseEnter(object sender, EventArgs e)
{
    Panel1_MouseEnter(this, EventArgs.Empty);
}

private void Label1_MouseLeave(object sender, EventArgs e)
{
    Panel1_MouseLeave(this, EventArgs.Empty);
}

CodePudding user response:

Try that

private void label_MouseEnter(object sender, EventArgs e)
{
    panel.RaiseEvent(new MouseEventArgs(Mouse.PrimaryDevice, 0) { RoutedEvent = Mouse.MouseEnterEvent });
}



  • Related