#683 – MouseUp Can Happen in Different Control from MouseDown

You can press a mouse button down while the mouse pointer is located in one control, move the mouse, and then release the mouse button with the mouse pointer located in a different control.  When you do this, the first control will receive the MouseDown event and the second control will receive the MouseUp event.

    <StackPanel>
        <Label Content="Sistine" Background="ForestGreen" Padding="10,20"
               MouseDown="Label_MouseDown" MouseUp="Label_MouseUp"/>
        <Label Content="Buonarroti" Background="Peru" Padding="10,20"
               MouseDown="Label_MouseDown" MouseUp="Label_MouseUp"/>
    </StackPanel>

 

        private void Label_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Label l = sender as Label;
            Console.WriteLine(string.Format("MouseDown on {0}", l.Content));
        }

        private void Label_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label l = sender as Label;
            Console.WriteLine(string.Format("MouseUp on {0}", l.Content));
        }

Advertisement

#657 – Detecting Double Clicks in User Interface Elements

You can react to a user double-clicking on a user interface element by handling one of the mouse button events and checking the MouseButtonEventArgs.ClickCount field.  When a user double-clicks on an element, all of the mouse down and mouse up events will be fired twice.  During the second round of events, the ClickCount property will have a value of 2.

Here’s an example, where we instrument the MouseDown and MouseUp events for a Label.

When we just click on the element, we see a MouseDown and a MouseUp event, with a ClickCount of 1.

If we double-click on the Label, we’ll see two sets of events.  The second MouseDown event will report a ClickCount of 2.

 

We could distinguish between double-clicking with the left mouse button vs. the right mouse button by handling either the MouseLeftButtonDown and MouseRightButtonDown events.