That error message is correct. The EventArgs
class, an instance of which you're accessing through the e
parameter, does not contain a Location
property.
Unfortunately, that's all you get with the Click
event. You need to switch to handling the MouseClick
event instead, which passes a MouseEventArgs
object with a Location
property. This is rather simple to do, you only need to update the name of the handler method and the code that attaches your handler to the event (possibly located in the designer-generated code-behind file).
Alternatively, you can retrieve the current location of the mouse pointer using the Cursor.Current
property. This is often "good enough", but do keep in mind a couple of things:
The
Click
event is not only raised in response to mouse events, but also in certain other cases, like when the control is focused and the user presses the Enter key. In those cases, the current location of the mouse pointer may be completely meaningless.That's why the
MouseClick
event is a better option. Not only does it give you the location information for free, but it is only raised in response to mouse events, when theLocation
property will be meaningful.The user could have moved the mouse between the time that the
Click
event was generated and the time that your event handler executes, which means thatCursor.Current
returns a different location than where the user originally clicked. In most cases this is not a significant distance, but it could be.