[GIS] Executing ArcMap AddIn function when there is mouse click on map

arcgis-10.0arcmaparcobjectsvbvisual studio

I am developing an add-in for ArcMap 10.0 where I need to execute code when there is a click on the map open. How do I do this? I am using VB in Visual Studio 2010. I also need to get the coordinates from the click event.

Best Answer

If you have created an add-in and added a ESRI.ArcGIS.Desktop.AddIns.Tool to it then stub out the following event:

Protected Overrides Sub OnMouseDown(ByVal arg As
    ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)

' --write your function here--

End Sub

arg.X and arg.Y will be the coordinates where the user clicked.

You will most likely want to convert the x and y values to the transformation used by the display:

Dim mxDoc As IMxDocument = CType(My.ArcMap.Application.Document, IMxDocument)   
Dim clickedPoint As ESRI.ArcGIS.Geometry.IPoint =
    mxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y)
Related Question