views:

652

answers:

3

Is there a way to tell what x/y coordinates were clicked in a FORMS application?

+2  A: 

The MouseDown, MouseUp and MouseClick events all return the X and Y coordinates of the action.

heavyd
+1  A: 

Look at System.Windows.Forms.Control.MousePosition (static property)

leppie
Although the MousePosition does get the current mouse position, this can be unreliable when testing where a user clicks. The user can move the mouse before execution reaches your code where you check the property and you can get undesired results. The control events are much more reliable.
heavyd
+2  A: 

Take a look at the MouseEventArgs class. Specifically the GetPosition method. The example on MSDN is using onMouseMove, but you should be able to do the same with onMouseClick. Or just use the MouseClick event of the form.

E.g. using the MouseClick event:

On your form:

this.MouseClick += new MouseEventHandler(myForm_MouseClick);

void myForm_MouseClick(object sender, MouseEventArgs e)
{
    int myX = e.X;
    int myY = e.Y;
}
Jason Down