[GIS] Activating custom ITool from form without adding it to ArcMap AddIn toolbar

arcgis-10.0arcobjectsctoolbartools

I'm working on an add-in for ArcMap 10.0 that adds a toolbar to ArcMap. One command (OpenModelessDialogCommand) button on that toolbar opens a modeless WinForms dialog, from which a tool (MyTool) can be activated in order to e.g. select a feature on the map.

My Config.esriaddinx contains these command and toolbar declarations:

<Commands>
  <!-- this is the command that opens the modeless WinForms form, from where
       MyTool is available: -->
  <Button id="OpenModelessFormCommand" ... />
  <!-- MyTool is not directly referenced in any toolbar defined in this file: -->
  <Tool id="MyTool" class="MyTool" ... />
</Commands>
<Toolbars>
  <Toolbar ...>
    <Items>
      <Button refID="OpenModelessFormCommand" />
    </Items>
  </Toolbar>
</Toolbars>

What I'm having problems with is activating MyTool in the form. All I've found on the internet is code samples along the lines of:

// get a reference to an instance of MyTool:
ICommandItem myTool = ArcMap.Application.Document.CommandBars.Find("MyTool");
 // activate MyTool: 
ArcMap.Application.CurrentTool = myTool;

However, this apparently requires that MyTool actually appears in a command bar (e.g. toolbar) of my add-in. But that's not the case. So, I've tried this next:

ITool myTool = new MyTool();
ArcMap.Application.CurrentTool = myTool;  // Type mismatch! An ICommandItem is expected.

I've even looked into adding an invisible AxToolbarControl to my form and adding a button for MyTool there; but then I'm running into problems on how to connect that toolbar (via SetBuddyControl) to the opened document's map. I don't want the tool to function in a separate AxMapControl, I want it to work directly with the main map shown in ArcMap.

Question:
How do I activate a custom tool that is not added to any toolbar (or other command bar, for that matter)?

Best Answer

This worked for me using ArcGIS 10 SP1. My custom tool is not on a toolbar:

    Dim UIDCls As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass()
    ' id property of menu from Config.esriaddinx document
    UIDCls.Value = "ClassLibraryAddin_MyTool"
    Dim document As ESRI.ArcGIS.Framework.IDocument = My.ArcMap.Document
    Dim commandItem As ESRI.ArcGIS.Framework.ICommandItem = TryCast(document.CommandBars.Find(UIDCls), ESRI.ArcGIS.Framework.ICommandItem)
    If commandItem Is Nothing Then
        Exit Sub
    End If
    My.ArcMap.Application.CurrentTool = commandItem