user3788761
user3788761

Reputation: 55

How do i get the mouse coordinates X and Y in form1 mouse enter event?

private void Form1_MouseEnter(object sender, EventArgs e)
{

}

e doesn't have the properties X and Y.

I want when the mouse cursor move over a control it will do something.

Upvotes: 2

Views: 4188

Answers (3)

DmitryG
DmitryG

Reputation: 17848

Use the Control.MousePosition static property as follows:

void Form1_MouseEnter(object sender, EventArgs e) {
    Point screenPosition = MousePosition; 
    Point clientPosition = PointToClient(screenPosition);
}

Upvotes: 3

ArtK
ArtK

Reputation: 1185

private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        Point p = e.Location;
    }

Upvotes: 2

D Stanley
D Stanley

Reputation: 152596

You can get the current cursor position using the Cursor.Current static property:

var x = Cursor.Current.Position.X;
var y = Cursor.Current.Position.Y;

Note that the MouseEnter event only fires when the cursor enters the control boundary. That may be what you want, but your last sentence seems to indicate you want to know when the mouse moves within a control. In that case, MouseMove may be a more appropriate event to handle.

Upvotes: 7

Related Questions