cannot find OnClick() method in Tool

4784
6
01-20-2015 09:35 AM
MariaMaldini
New Contributor

I am developing a Tool using Add-Ins in order to get the coordinate where user clicks on map ( through OnMouseDown event), but I want to do some operations before getting coordinates in another method. I try to explain my problem with a simple example. for example, I want to see a message box that will be called from a method ( msg method) and then user click on map and get coordinates using OnMouseDown event. my problem is that the first time that i click on tool's icon i see a message box (" hello") but the second time that i click on tool's icon, it does not show message box and tool just call OnMouseDown event.

( as i told, i do not want to have message box in  OnMouseDown event)

please help, thanks in advance

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Windows.Forms;

namespace msg

{

    public class msg : ESRI.ArcGIS.Desktop.AddIns.Tool

    {

        public msg()

        {

            MessageBox.Show("hello");

        }

        protected override void OnMouseDown(MouseEventArgs arg)

        {

            //get map coordinate

        } 

        protected override void OnUpdate()

        {

            Enabled = ArcMap.Application != null;

        }

    }

}

0 Kudos
6 Replies
MariaMaldini
New Contributor

is there any OnClick event or other way that i can call my message box every time that i click on tool's icon?

0 Kudos
MahtabAlam1
Occasional Contributor

You are calling show message in tool's constructor and it will show only once. You might want to look at OnActivate method/event.

0 Kudos
MariaMaldini
New Contributor

thanks for your quick responses. i am using c# so i converted the mentioned code to c#. but still i have the previous problem. is there any mistake to convert codes?

namespace msg

{

    public class msg : ESRI.ArcGIS.Desktop.AddIns.Tool

    {

        public msg()

        {         

        }

       protected override void OnActivate()

        {

            base.OnActivate();

            MessageBox.Show("hello");

        }

        protected override bool OnDeactivate()

        {

            bool functionReturnValue = false;

            functionReturnValue = true;

            this.Enabled = false;

            return functionReturnValue;

        }

        protected override void OnMouseDown(MouseEventArgs arg)

        {

            //get map coordinate

        }

        protected override void OnUpdate()

        {

            Enabled = ArcMap.Application != null;

        }

    }

}

0 Kudos
MariaMaldini
New Contributor

i changed the codes as following lines but i had the mentioned problem. i could not find any material about the way OnActivate and OnDeactivate methods work. is there anyone who can help me?

        protected override void OnActivate()

        {

            base.OnActivate();

            MessageBox.Show("hello");

        }

        protected override bool OnDeactivate()

        {

            return base.OnDeactivate();

        }

0 Kudos
MahtabAlam1
Occasional Contributor

OnActivate will be called when you activate the tool. But if you already have your tool active it won't do anything. So you need to set current tool as null once you are done with your logic. Something like this:

protected override void OnMouseDown(MouseEventArgs arg)
{
  try
  {
       //get map coordinate
  }
  catch(ex)
  {
       //handle exception
  }
  finally
  {
       ArcMap.Application.CurrentTool = null;
  }
}

Hope that it gives you what you want.

MariaMaldini
New Contributor

Really thanks, "finally { }" part solved my problem. Thanks a lot

0 Kudos