MouseEventArgs.Location Property

Definition

Gets the location of the mouse during the generating mouse event.

C#
public System.Drawing.Point Location { get; }

Property Value

A Point that contains the x- and y- mouse coordinates, in pixels, relative to the upper-left corner of the control.

Examples

The following code example uses the Location property to track left mouse clicks and draw a series of straight line segments in response to user input. The example does not persist the drawn lines if you hide the form and then redisplay it; this code was omitted for simplicity.

C#
Point firstPoint;
Boolean haveFirstPoint;

public void EnableDrawing()
{
    this.MouseDown += new MouseEventHandler(Form1_MouseDownDrawing);
}

void Form1_MouseDownDrawing(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (haveFirstPoint)
    {
        Graphics g = this.CreateGraphics();
        g.DrawLine(Pens.Black, firstPoint, e.Location);
        haveFirstPoint = false;
    }
    else
    {
        firstPoint = e.Location;
        haveFirstPoint = true;
    }
}

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also