How to Embed BaseTool to winform in ArcObjects

add-inarcmaparcobjectsc

I am new to C# and still doing research. I open a windows form from a dockablewindow menu in ArcMap. I also have a button in WindowsForm, and when I click this button, I want to draw a feature on the map (point, polygon, line… whatever).

I think I should add a basetool and define the guid that basetool has as currenttool. I'm trying to do this using ICommandItem for this but I'm getting an error.

It returns null because it cannot find the Guid specified in the code.

BaseTool.cs:

namespace MaksAddIn
{
/// <summary>
/// Summary description for Tool1.
/// </summary>
[Guid("03e2069c-7c3f-4156-aeda-36aa3c936221")] //using this guid
[ClassInterface(ClassInterfaceType.None)]
[ProgId("MaksAddIn.Tool1")]
public sealed class Tool1 : BaseTool
{
    protected IApplication m_Application;
    public ICommandItem RollbackTool { get; set; }
    private IHookHelper m_hookHelper;
    public IMxDocument m_mxDoc;
    public MxDocument m_mxDocument;
    public Map m_map;
    public PageLayout m_pageLayout;
    private System.Windows.Forms.Button button1;
    public IFeatureClass featureClass;
    public Tool1()
    {
        //
        // TODO: Define values for the public properties
        //
       }
    public override void OnMouseDown(int Button, int Shift, int X, int Y)
    {
        MessageBox.Show(X.ToString(), Y.ToString());
    }

Winform.cs:

  private void Click(object sender, EventArgs arg)
    {
        IApplication _mApplication = ArcMap.Application;
        ICommandBars documentBars = _mApplication.Document.CommandBars;
        UID cmdID = new UIDClass();
        cmdID.Value = "{03e2069c-7c3f-4156-aeda-36aa3c936221}";
        ICommandItem cmdItem = documentBars.Find(cmdID); //cmdItem null error is here
        MessageBox.Show(cmdItem.ToString());
        
        _mApplication.CurrentTool = cmdItem;

    }

does not find the specified guid in the application.

Best Answer

I believe you need to specify the two optional parameters to the Find method: documentBars.Find(cmdID, false, false);

From the Documentation:

  • noRecurse Use False to search all menus and toolbars for the item; otherwise use True. [Optional]
  • noCreate Use False if you want the item to get instantiated if it isn't already; otherwise use True. [Optional]

Alternatively, if you are writing an Add-in, you can add a tool by right clicking on the project > Add New Item > ArcGIS Desktop Add-ins > Add-In Component > Add > Select the Tool component. Then, use new UIDClass { Value = ThisAddIn.IDs.YourTool }

Related Question