WPF - Queued mouse event trigger on next screen

Intro I have a WPF application. When i click/double click on a button to show next screen, this is captured with both MouseLeftButtonDown and MouseLeftButtonUp. - MouseLeftButtonDown is making the button darker - MouseLeftButtonUp is sending me to next screen

The problem: If i "spam" or sometimes just click 2-3 (we say 3 times in this case) times on the button, it's start loading the next screen. When the next screen is shown, two mouse clicks is left in the queue and if the mouse is over another new button at the second screen, this is clicked on.

I tried with stuff like:

    void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        Mouse.OverrideCursor = Cursors.Wait;
        if (this.IsVisible && this.IsLoaded)
            OnButtonPatient();
    }

but both properties is set to true. I guess thats right since i can see the button when the mouse event triggers. The events can be explained like:

  1. 3 mouse clicks
  2. mouse cursor = waiting cursor
  3. next screen is loaded
  4. mouse cursor = normal
  5. mouse cursor = waiting cursor
  6. next button is clicked

How can i handle this? I dont want mouse event that happend on a previously screen follow on to my next screen. Greetings!

Upvotes: 1

Views: 488

Answers (2)

lerner1225
lerner1225

Reputation: 902

I tried a simple example using StackPanel as control. Although I am not sure why you are not using Button Click event if you are using a button (Why move on ButtonUp?)

private int count = 0;
        private void StackPanel_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (count == 0 && (sender as StackPanel).IsMouseOver)
            {
                Mouse.OverrideCursor = Cursors.Wait;
                count++;
            }

            e.Handled = true;
        }

        private void StackPanel_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (count == 1 && (sender as StackPanel).IsMouseOver)
            {
                Mouse.OverrideCursor = Cursors.Arrow;
                count++;
            }

            e.Handled = true;
        }

Upvotes: 1

Sean Beanland
Sean Beanland

Reputation: 1118

I would suggest checking the IsMouseOver property on your first window/screen when the MouseUp event fires. This would let you filter out events where the new window is blocking the first.

void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    Mouse.OverrideCursor = Cursors.Wait;
    if (this.IsVisible && this.IsLoaded && this.IsMouseOver)
        OnButtonPatient();
}

Upvotes: 0

Related Questions