Skip to main content

Microsoft Silverlight

Unanswered Question MouseLeave is not always calledRSS Feed

(0)

VladF
VladF

Member

Member

216 points

87 Posts

MouseLeave is not always called

The MouseLeave event is not always called when I move mouse fast.

Here you will find an example application showing this bug. It has 2 text blocks counting how many times MouseLeave was called and how many times it failed and MouseMove was used to cancel mouse capturing.

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace SilverlightProject10

{

public partial class Page : Canvas

{

private Ellipse area;

private const double AreaSize = 8;

public void Page_Loaded(object o, EventArgs e)

{

// Required to initialize variables

InitializeComponent();

MouseLeftButtonDown +=
new MouseEventHandler(Page_MouseLeftButtonDown);

}

void ShowArea(Point p)

{

area =
new Ellipse { Width = AreaSize, Height = AreaSize, Fill = new SolidColorBrush(Colors.Red) };

area.SetValue(LeftProperty, p.X - area.Width / 2);

area.SetValue(TopProperty, p.Y - area.Height / 2);

area.MouseLeave +=
new EventHandler(area_MouseLeave);

area.MouseLeftButtonUp += new MouseEventHandler(area_MouseLeftButtonUp);

area.MouseMove += new MouseEventHandler(area_MouseMove);

Children.Add(area);

area.CaptureMouse();

}

void HideArea()

{

area.ReleaseMouseCapture();

Children.Remove(area);

area =
null;

}

void Page_MouseLeftButtonDown(object sender, MouseEventArgs e)

{

ShowArea(e.GetPosition(
this));

}

void area_MouseMove(object sender, MouseEventArgs e)

{

var r = new Rect(0, 0, area.Width, area.Height);if (!r.Contains(e.GetPosition(area)))

{

HideArea();

tb2.Text = (
int.Parse(tb2.Text) + 1).ToString();

}

}

void area_MouseLeftButtonUp(object sender, MouseEventArgs e)

{

HideArea();

}

void area_MouseLeave(object sender, EventArgs e)

{

HideArea();

tb1.Text = (
int.Parse(tb1.Text) + 1).ToString();

}

}

}

 

<Canvas x:Name="parentCanvas"

xmlns="http://schemas.microsoft.com/client/2007"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Loaded="Page_Loaded"

x:Class="SilverlightProject10.Page;assembly=ClientBin/SilverlightProject10.dll"

Width="640"

Height="480"

Background="Orange"

>

<
TextBlock x:Name="tb1" Text="0" FontSize="32"/>

<TextBlock x:Name="tb2" Text="0" FontSize="32" Canvas.Top="40" Foreground="Red"/>

</Canvas>

Pirashanthan Arasaratnam
Pirashan...

Member

Member

14 points

15 Posts

Re: MouseLeave is not always called

Hello,

One point is that it may be due to the fact that you are moving the mouse pointer fast and another may be due to the fact that your ellipse and textblocks are overlapping each other.

To overcome this problem give Canvas .Zindex and absolute positioning like Canvas.Left and Canvas.Top properties to your ellipse and textblocks and check whether it works fine or not. 

Regards 

Pirashanthan A

VladF
VladF

Member

Member

216 points

87 Posts

Re: MouseLeave is not always called

Ellipses do not overlap with text blocks in most cases - just do not move ellipses over text blocks. And they have absolute coordinates set - otherwise you would be able to move them. Z-Index won't help in this case.

I consider this a bug but see no response from Microsoft.

swirlingmass
swirling...

Participant

Participant

1348 points

385 Posts

Re: MouseLeave is not always called

I think the problem is that the mouse never enters the ellipse in the first place.  When you're moving the mouse fast, I guess it can already be off the ellipse by the time it is added to the canvas.  If you add a MouseEnter counter, I think you will see that the times MouseLeave is not fired are also the times MouseEnter is not fired.

VladF
VladF

Member

Member

216 points

87 Posts

Re: MouseLeave is not always called

Did you run the sample app I posted? You can just press the left mouse button - ellipse will be created, mouse will be in the middle of the ellipse - then you move mouse fast enough and MouseLeave is not called even so mouse left ellipse boundaries.

swirlingmass
swirling...

Participant

Participant

1348 points

385 Posts

Re: MouseLeave is not always called

Yes, I did run your app.  For me, the only times the MouseLeave event did not fire were when I was moving the mouse while clicking.  I tried at least a hundred times creating an ellipse while the mouse was still, and then moving off of it as fast as I could, but it always worked fine that way.

VladF
VladF

Member

Member

216 points

87 Posts

Re: MouseLeave is not always called

I just tried it again. From 30 attempts I got 4 failed MouseLeaves. And the speed of mouse movement is not that big (I use laptop and touchpad). What is also interesting is that in one particular direction failures happen more offten - when I move mouse to the left-bottom.

swirlingmass
swirling...

Participant

Participant

1348 points

385 Posts

Re: MouseLeave is not always called

Well, since I'm not able to reproduce it, I'm inclined to think it's something particular with your system.  Have you tried it on other machines?

It would be nice if we could get someother people out there to say whether or not they are able to reproduce it...

wjchristenson2
wjchrist...

Member

Member

104 points

51 Posts

Re: MouseLeave is not always called

I'm having a similar issue.

I have a StackPanel that has 7 user controls in it (of the same type).  Each UserControl has a Slider control in it.  I toggle the visibility of the Slider inside my UserControl to visible when the mouse hovers over the UserControl.

 If you run the mouse quickly over the UserControls, the MouseLeave event does not always fire.  It tends to do this more when I move the mouse quickly over the Slider section of the UserControls.  I've noticed that if I put a lot of padding around the Slider, the UserControl is more apt to fire the MouseLeave event.

Bill - Senior .NET Developer
A Mostly Developers Blogger

Please remember to click “Mark as Answer” on the post that helps you.

Simbalight
Simbalight

Member

Member

245 points

87 Posts

Re: MouseLeave is not always called

VladF:

I didn't try your code but from the postings I can see that you create elements at the cursor's position. The problem is that MouseEnter is never called, when you create the element. Then when you move the cursor fast enough, the cursor will never hit the element in it's movement. MouseEnter won't be called and thus MouseLeave will neither because the UI never recognizes the cursor was over the element. Up to now I don't know any workaround.

Would be great to get some feedback from MS if this is working as intended.

wjchristenson2
wjchrist...

Member

Member

104 points

51 Posts

Re: MouseLeave is not always called

I tested this on SL2RC0 and I cannot reproduce the bug.  Anyone else have better luck with SL2RC0?

Bill - Senior .NET Developer
A Mostly Developers Blogger

Please remember to click “Mark as Answer” on the post that helps you.

avbersSL
avbersSL

Member

Member

164 points

77 Posts

Re: MouseLeave is not always called

Seeing this too..

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities