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 ?

Re: Form level OnMouseLeave by Nelson

Nelson
Tue Oct 18 04:41:33 CDT 2005

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



"JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
news:u7VPPK80FHA.560@TK2MSFTNGP12.phx.gbl...
> 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 ?
>



Re: Form level OnMouseLeave by JezB

JezB
Tue Oct 18 04:50:42 CDT 2005

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

"Nelson Guerra" <Nelson@Don'tuse.com> wrote in message
news:%23b4uac80FHA.3924@TK2MSFTNGP14.phx.gbl...
> 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
>
>
>
> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
> news:u7VPPK80FHA.560@TK2MSFTNGP12.phx.gbl...
>> 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 ?
>>
>
>



Re: Form level OnMouseLeave by Henrry

Henrry
Tue Oct 18 06:35:04 CDT 2005

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

"JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
news:Oh4Hhj80FHA.700@TK2MSFTNGP10.phx.gbl...
>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 ??
>
> "Nelson Guerra" <Nelson@Don'tuse.com> wrote in message
> news:%23b4uac80FHA.3924@TK2MSFTNGP14.phx.gbl...
>> 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
>>
>>
>>
>> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
>> news:u7VPPK80FHA.560@TK2MSFTNGP12.phx.gbl...
>>> 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 ?
>>>
>>
>>
>
>



Re: Form level OnMouseLeave by JezB

JezB
Tue Oct 18 06:36:53 CDT 2005

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.

"Henrry Pires" <Nelson@Don'tuse.com> wrote in message
news:O66e3b90FHA.1108@TK2MSFTNGP14.phx.gbl...
> why don't you try to do the handles of the panel and the form in the same
> function?
>
> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
> news:Oh4Hhj80FHA.700@TK2MSFTNGP10.phx.gbl...
>>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 ??
>>
>> "Nelson Guerra" <Nelson@Don'tuse.com> wrote in message
>> news:%23b4uac80FHA.3924@TK2MSFTNGP14.phx.gbl...
>>> 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
>>>
>>>
>>>
>>> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
>>> news:u7VPPK80FHA.560@TK2MSFTNGP12.phx.gbl...
>>>> 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 ?
>>>>
>>>
>>>
>>
>>
>
>



Re: Form level OnMouseLeave by JezB

JezB
Tue Oct 18 07:29:27 CDT 2005

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();
}

"JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
news:Oh4Hhj80FHA.700@TK2MSFTNGP10.phx.gbl...
>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 ??
>
> "Nelson Guerra" <Nelson@Don'tuse.com> wrote in message
> news:%23b4uac80FHA.3924@TK2MSFTNGP14.phx.gbl...
>> 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
>>
>>
>>
>> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
>> news:u7VPPK80FHA.560@TK2MSFTNGP12.phx.gbl...
>>> 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 ?
>>>
>>
>>
>
>



Re: Form level OnMouseLeave by JezB

JezB
Tue Oct 18 08:04:34 CDT 2005

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();
}

"JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
news:uxSIP890FHA.4032@TK2MSFTNGP15.phx.gbl...
> 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();
> }
>
> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
> news:Oh4Hhj80FHA.700@TK2MSFTNGP10.phx.gbl...
>>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 ??
>>
>> "Nelson Guerra" <Nelson@Don'tuse.com> wrote in message
>> news:%23b4uac80FHA.3924@TK2MSFTNGP14.phx.gbl...
>>> 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
>>>
>>>
>>>
>>> "JezB" <jezbroadsword@blueyonder.co.uk> wrote in message
>>> news:u7VPPK80FHA.560@TK2MSFTNGP12.phx.gbl...
>>>> 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 ?
>>>>
>>>
>>>
>>
>>
>
>