Accessing/Using ArcMaps Commands via C#

2407
1
Jump to solution
11-18-2014 04:45 PM
JamariPowers
New Contributor III

I am working with ArcObjects, Arc Map and C#. There is a tool used in ArcMap to select elements on a map document (esriArcMapUI.SelectTool). I want to be able to use this so that on a button click from my form, this tool becomes the currently active tool. I have looked at examples but I keep getting a Null reference object error when I click my button. Here's what I have so far:

Namespace MYPROJECT

{

   public class myButton : Button

   {

      IApplication app;

       public myButton()

       {

       }

protected override void onClick()

{

try

       {

        ICommandBars documentBars = app.Document.CommandBars;

        UID cmdID = new UIDClass();

        cmdID.Value = "{B7FA188F-EBE3-11D0-87FE-080009EC732A}";

        ICommandItem cmdItem = documentBars.Find(cmdID, false, false);

        app.CurrentTool = cmdItem;

   

        } catch (exception myException)

         {

              messageBox.Show("Error: " + myException);

         }

} }

}

I am working with map documents, so I may be using the wrong Arc Objects, but again, when I click my button, I get an error:

System.NullReferenceException: Object not set to an instance of an abject. So something is still null or not getting set. More tracing perhaps, but I am wondering if I am even going about this the right way. Any help on this would be greatly appreciated. Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
JamariPowers
New Contributor III

After hours and hours of research, I think I found a solution to my specific problem. I wanted to post an explanation and solution to what I had come up with, just in case anyone else ever has the same issue. Here goes:

So I wanted to capture one of ArcMaps tools/items from their toolbar. I wanted to capture the "Select Element" tool (the arrow). I first needed to get the GUID of the tool that I wanted to capture. Once I had the GUID, I would need to find it on ArcMaps toolbar and then access it/highlight it/show it, etc. The correct word would be Activate. For my simple test, I had a button that when clicked, it would select the "Select Element" tool. Code below:

UID myUid = new UID();

myUid.Value = "esriArcMapUI.SelectTool";

IDocument ThisDoc = ArcMap.Application.Document;

ICommandBars CommandBars = ThisDoc.CommandBars as ICommandBars;

CommandBars.Find(myUid);

ICommandItem myItem = CommandBars.Find(myUid) as ICommandItem;

myItem.Execute();

              

View solution in original post

0 Kudos
1 Reply
JamariPowers
New Contributor III

After hours and hours of research, I think I found a solution to my specific problem. I wanted to post an explanation and solution to what I had come up with, just in case anyone else ever has the same issue. Here goes:

So I wanted to capture one of ArcMaps tools/items from their toolbar. I wanted to capture the "Select Element" tool (the arrow). I first needed to get the GUID of the tool that I wanted to capture. Once I had the GUID, I would need to find it on ArcMaps toolbar and then access it/highlight it/show it, etc. The correct word would be Activate. For my simple test, I had a button that when clicked, it would select the "Select Element" tool. Code below:

UID myUid = new UID();

myUid.Value = "esriArcMapUI.SelectTool";

IDocument ThisDoc = ArcMap.Application.Document;

ICommandBars CommandBars = ThisDoc.CommandBars as ICommandBars;

CommandBars.Find(myUid);

ICommandItem myItem = CommandBars.Find(myUid) as ICommandItem;

myItem.Execute();

              

View solution in original post

0 Kudos