Click and the order of mouse events

A user presses a mouse button over a control and releases it, and four events follow in a fixed order: MouseDown, Click, MouseClick and MouseUp. The order is not obvious, and every WinForms programmer guesses it wrong once: Click comes before MouseUp, and MouseClick is the one that carries the button and the coordinates.

One click, four events

The order below is the one the documentation of the platform states for all controls except the four in the next section:

using System; using System.Drawing; using System.Windows.Forms; internal static class Program { [STAThread] private static void Main() { ApplicationConfiguration.Initialize(); Application.Run(new Form1()); } } internal sealed class Form1 : Form { private readonly Label _log = new() { Dock = DockStyle.Fill, AutoSize = false }; public Form1() { Text = "Event order"; ClientSize = new Size(360, 200); var button = new Button { Text = "Press me", Location = new Point(16, 16) }; button.MouseDown += (sender, e) => Append($"MouseDown {e.Button} at {e.Location}"); button.Click += (sender, e) => Append("Click"); button.MouseClick += (sender, e) => Append($"MouseClick {e.Button}"); button.MouseUp += (sender, e) => Append($"MouseUp {e.Button}"); Controls.Add(button); Controls.Add(_log); } private void Append(string line) { _log.Text = _log.Text.Length == 0 ? line : _log.Text + Environment.NewLine + line; } }

dotnet build app.csproj

Build succeeded.
    0 Warning(s)
    0 Error(s)

The listing is built and not run. Four handlers on one button, each appending a line to a label, is the smallest program that shows the order on a real machine. Two of the four events carry no position, which is why the handler that needs it subscribes to MouseClick or MouseDown.

The platform documents one more difference for a button: it paints itself pressed between the press and the MouseDown event, paints itself raised after the release and before Click, and raises Click, MouseClick and MouseUp in that order afterwards. A control that paints in its handlers can see each state.

A double click

A double click is not "two clicks". The platform raises the first press and release completely, then a second MouseDown and a DoubleClick and a MouseDoubleClick and a MouseUp. The full documented order is:

  1. MouseDown.
  2. Click.
  3. MouseClick.
  4. MouseUp.
  5. MouseDown again.
  6. DoubleClick, which depends on the ControlStyles.StandardDoubleClick style bit.
  7. MouseDoubleClick.
  8. MouseUp.

The consequence is the classic bug: a form that opens a dialog on Click opens it twice on a double click, because the first click already fired. A form that also handles DoubleClick has to decide which of the two clicks it means, and the usual answer is a short timer: the click waits, and a second click within the interval turns the pair into one action. The last section is that rule.

The controls that differ

Four kinds of control do not raise MouseDown and MouseUp for a click of their own, and the documented list is short enough to know:

The list matters when a handler is attached to the wrong event. A Click on a ListView that does nothing is usually a click in the empty space below the last row, and a right-click menu on a Button has to come from MouseUp and a check of e.Button, because Click never fires for the right button on those four controls.

The timing that runs

The decision "is this the second click of a double click" is a subtraction against an interval. The interval is the user's, SystemInformation.DoubleClickTime in milliseconds, and the arithmetic that uses it runs here with the interval passed in:

using System; var threshold = 500; var previous = -1; var presses = new[] { (Time: 100, InControl: true), (Time: 250, InControl: true), (Time: 900, InControl: true), (Time: 950, InControl: false), (Time: 1600, InControl: true), }; foreach (var press in presses) { if (!press.InControl) { Console.WriteLine($"{press.Time,4} ms: released outside, MouseUp only"); continue; } var second = previous >= 0 && press.Time - previous <= threshold; Console.WriteLine($"{press.Time,4} ms: {(second ? "DoubleClick on release" : "Click on release")}"); previous = press.Time; } Console.WriteLine($"threshold used: {threshold} ms");

dotnet run

 100 ms: Click on release
 250 ms: DoubleClick on release
 900 ms: Click on release
 950 ms: released outside, MouseUp only
1600 ms: Click on release
threshold used: 500 ms

The output is the real run. A press 150 milliseconds after the first is the second of a pair, a press 650 milliseconds later is a new click, and a release outside the control is neither: it is the MouseUp with no Click that the previous section describes. The guard previous >= 0 is the first press, which has nothing to compare with, and the comparison is <= because the documented interval includes its end.

Exercises

  1. Add a press at 1400 ms and work out its line before running the program.
  2. Change the comparison to < and run it with a press exactly 500 ms after the previous one.
  3. Explain from the documentation why a dialog opened in a Click handler opens twice on a double click.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. In what order do the four click events come for one click?

    Answer

    MouseDown, Click, MouseClick, MouseUp

    Click is raised when the button is released, before MouseUp, and MouseClick follows it.

  2. Which event carries the button and the coordinates of a click?

    Answer

    MouseClick, with MouseEventArgs

    Click passes a plain EventArgs; MouseClick passes the mouse data.

  3. How many times does Click fire on a double click?

    Answer

    Twice, once per press and release

    The first click is complete before the second MouseDown, which is why a dialog opens twice.

  4. Which event is raised for the second press of a double click?

    Answer

    DoubleClick, after a second MouseDown

    The documented order is MouseDown, DoubleClick, MouseDoubleClick, MouseUp.

  5. Which controls raise no click events for a right click?

    Answer

    Button, CheckBox, RadioButton and ComboBox

    Those four raise Click and MouseClick for the left button only.

  6. Which controls raise the click events only over their items?

    Answer

    ListView and TreeView

    A click on the empty area of a list or tree raises nothing.

  7. What happens when the pointer leaves a button while the button is held?

    Answer

    MouseUp is raised and Click is not

    That is why dragging off a button cancels the click.

  8. What did the run print for the press at 250 ms?

    Answer

    DoubleClick on release

    It is within 500 ms of the press at 100 ms.

  9. Where does the interval for a double click come from on a real machine?

    Answer

    SystemInformation.DoubleClickTime

    It is the user's setting, in milliseconds, which is why the program takes it as a parameter.

  10. Why is previous >= 0 guard needed in the classifier?

    Answer

    The first press has no earlier press to compare with

    Without it the first press compares against the initial -1.

Every listing on this page is plain .NET code with no window in it: it was built and run with the .NET SDK 10.0.303 on Alpine Linux 3.24.3 (x86_64) in a Docker container, and the output printed below each program is that run. The WinForms pages of this course are built for net10.0-windows against Microsoft.WindowsDesktop.App.Ref 10.0.15 and never run on Linux: WinForms needs Windows.

Article author: Arthur Isaev