10

I have Form subclass with handlers for MouseHover and MouseLeave. When the pointer is on the background of the window, the events work fine, but when the pointer moves onto a control inside the window, it causes a MouseLeave event.

Is there anyway to have an event covering the whole window.

(.NET 2.0, Visual Studio 2005, Windows XP.)

Ghasem
  • 14,455
  • 21
  • 138
  • 171
billpg
  • 3,195
  • 3
  • 30
  • 57

4 Answers4

11

Ovveride the MouseLeave event to not trigger so long as the mouse enters a child control

    protected override void OnMouseLeave(EventArgs e)
    {
        if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
            return;
        else
        {
            base.OnMouseLeave(e);
        }
    }
Ryan
  • 111
  • 1
  • 2
6

There is no good way to make MouseLeave reliable for a container control. Punt this problem with a timer:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        timer1.Interval = 200;
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Enabled = true;
    }

    private bool mEntered;

    void timer1_Tick(object sender, EventArgs e) {
        Point pos = this.PointToClient(Cursor.Position);
        bool entered = this.ClientRectangle.Contains(pos);
        if (entered != mEntered) {
            mEntered = entered;
            if (!entered) {
                // Do your leave stuff
                //...
            }
        }
    }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
6

When the mouse leave event is fired one option is to check for the current position of the pointer and see if it within the form area. I am not sure whether a better option is available.

Edit: We have a similar question which might be of interest to you. How to detect if the mouse is inside the whole form and child controls in C#?

Community
  • 1
  • 1
Shoban
  • 22,920
  • 8
  • 63
  • 107
  • So if I register the same handler for MouseEnter and MouseLeave for *all* controls inside a window (including inside panels), then have that handler check the mouse position to detect a change of inside/outside and call the "real" enter/leave handler. Good idea, thanks. – billpg Jan 06 '10 at 18:33
  • I added an example of how to use just one MouseEnter and MouseLeave event for the entire form. Let me know if that works for you – SwDevMan81 Jan 06 '10 at 19:49
  • Good idea. But if the contained Control is on the border it won't work. (Give credit [here](http://stackoverflow.com/a/19361582/939213) to the user who noticed it.) – ispiro Dec 28 '13 at 22:12
0

On your user control create a mousehover Event for your control like this, (or other event type) like this

private void picBoxThumb_MouseHover(object sender, EventArgs e)
{
    // Call Parent OnMouseHover Event
    OnMouseHover(EventArgs.Empty);
}

On your WinForm which hosts the UserControl have this for the UserControl to Handle the MouseOver so place this in your Designer.cs

this.thumbImage1.MouseHover += new System.EventHandler(this.ThumbnailMouseHover);

Which calls this method on your WinForm

private void ThumbnailMouseHover(object sender, EventArgs e)
{

    ThumbImage thumb = (ThumbImage) sender;

}

Where ThumbImage is the type of usercontrol

Taryn
  • 242,637
  • 56
  • 362
  • 405
fkerrigan
  • 270
  • 2
  • 9