LucieBoucher

C# OnMouseDown returning an IPoint?

Discussion created by LucieBoucher on Mar 28, 2012
Latest reply on Sep 11, 2012 by kenbuja
Hi,

I'm a newbie to developping add-ins in C#, probably mixing many concepts and principles...

I have built a simple tool that gets the coordinates when the user clicks on the map.  For now, I have a GetCoord.cs file with the following code :

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Geometry; //For geometry IPoint
using ESRI.ArcGIS.Carto; //For ActiveView
using ESRI.ArcGIS.Display; //For IScreenDisplay


namespace myProject
{
    public class GetCoord: ESRI.ArcGIS.Desktop.AddIns.Tool
    {
        public GetCoord()
        {
        }

        protected override void OnUpdate()
        {
        }

        protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            try
            {
                //Get the active view
                IActiveView activeView = ArcMap.Document.ActiveView;
                IScreenDisplay screenDisplay = activeView.ScreenDisplay;

                //Get the point when the user clicks on the map
                IPoint myPoint = screenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
                System.Windows.Forms.MessageBox.Show("X coordinate from the IPoint: " + myPoint.X);
                System.Windows.Forms.MessageBox.Show("Y coordinate fom the IPoint: " + myPoint.Y);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Problem to get the coordinates: " + e.Message);
            }
        }
    }
}


This codes works, I have the tool on my toolbar, I click on the tool and then on the map, and the coordinates are written in the message box. :)

What I would like to do next is to use this tool within other methods and classes, to use the coordinates for different purposes.  For instance, I would like to create a NewPoint.cs button, which would call the GetCoord.OnMouseDown() function and use the provided coordinates to create a new point in a feature class. 

I was therefore wondering if I could modify my code to get something like this :
public override IPoint OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
{
    //Get the active view
    IActiveView activeView = ArcMap.Document.ActiveView;
    IScreenDisplay screenDisplay = activeView.ScreenDisplay;
    //Get the point when the user clicks on the map
    IPoint myPoint = screenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
    return myPoint;
}


and then, later in another cs file :
Ipoint myNewPoint = GetCoord.OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg);


Thank you for your help!

Outcomes