Mouse leave event, on child controls

I have a usercontrol with a label and a picturebox on it, on the usercontrol itself there is a mouseleave event which change the background color of the usercontrol.

My question is, how do i either make the child controls react to this or ignore it - as it is now the mouseleave will be triggered when the mouse enters forinstance the label :-(

Regards


Answer this question

Mouse leave event, on child controls

  • dotnetto

    In the Load event of your usercontol you iterate over all controls and wire the MouseLeave event to the method:


    private void UserControl1_Load( object sender, EventArgs e )
    {
    this.MouseLeave += new EventHandler(Control_MouseLeave);

    foreach(Control control in Controls)
    {
    control.MouseLeave += new EventHandler(Control_MouseLeave);
    }
    }

    private void Control_MouseLeave( object sender, EventArgs e )
    {
    // TODO: Handle the mouse leave.
    }




  • Swarna

    This is the correct way, you must just check if you don't set the same color twice:


    if( !this.BackColor.Equals( Color.Red ) )
    {
    this.BackColor = Color.Red;
    }



    When you set a lot of colors you can also use the LockWindowUpdate api or the SuspendLayout method of the Control class.


  • SithKitty

    Thank you for the quick and helpfull reply

    I have tried this, put the usercontrols event on the child controls too, but this creates a bit of flicker with the colors as it triggers from usercontrol onto child control.

    Maybe this is the correct soultion, I was just hoping for something a little better.

  • Mouse leave event, on child controls