Form level OnMouseLeave

J

JezB

I'm overriding OnMouseLeave at Form level:

protected override void OnMouseLeave(EventArgs e)
{
this.Close();
}

The purpose of this, as you may suspect, is to close the form when the user
moves the mouse off it. However, this seems to fire whenever the mouse moves
over any control on the form, I imagine because the mouse is "leaving" the
form's base surface.

How can I do what I'm trying to do ?
 
N

Nelson Guerra

hello
Try this

Private Sub Form_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.MouseLeave, UltraGrid1.MouseLeave

Dim p As Point = Me.MousePosition

p = PointToClient(p)

If p.X > DisplayRectangle.Width OrElse p.Y > DisplayRectangle.Height OrElse
p.X < 0 OrElse p.Y < 0 Then

Me.Close()

End If

End Sub



the code is in VB but i think you undertand it
 
J

JezB

I was already thinking along similar lines Nelson, thanks.

It will work in that it will ignore the times the MouseLeave (or
OnMouseLeave) fires on leaving the form's surface to go over another control
within the form.

But, and this is a big BUT, I have just found that, if the default position
of the form at startup is such that the mouse is already over a control, and
that control is next to the form's edge, moving the mouse off the form
without leaving that control does not fire the form's MouseLeave event at
all ! The most obvious case is when the form contains a docked panel that
fills the form's surface.

I think we have to think of a more generic solution. Or someone does !
Anybody ??
 
H

Henrry Pires

why don't you try to do the handles of the panel and the form in the same
function?
 
J

JezB

Yes I could, and that would work up to a point. It still remains a problem
if there is a control right at the edge of the panel and the mouse leaves it
via that control - again it won't fire.
 
J

JezB

Solution appears to be along the lines you suggested, but to attach the same
MouseLeave event to all controls on the form as well as the form itself.
This can be done by a call to a recursive routine at the end of the form's
constructor:

public Form1()
{
InitializeComponent();
AttachMouseLeaveEvents(this);
}
private void AttachMouseLeaveEvents(Control c)
{
c.MouseLeave += new EventHandler(genericMouseLeave);
if (c.HasChildren)
foreach (Control x in c.Controls)
AttachMouseLeaveEvents(x);
}
private void genericMouseLeave(object sender, EventArgs e)
{
if (!this.Bounds.Contains(Cursor.Position))
this.Close();
}
 
J

JezB

Correction: (to give a little leeway to the bounds check, moving the mouse
off slowly did not work with the previous version)

private void genericMouseLeave(object sender, EventArgs e)
{
Rectangle innerBounds = new Rectangle(this.Bounds.X + 2, this.Bounds.Y +
2, this.Width - 4, this.Height - 4);
if (!innerBounds.Contains(Cursor.Position)) this.Close();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top