2

I am writting an addin using ArcObjects C#. I have setup the following listeners, but they are not being hit.

My code at the top of the addin.Extension class is:

...
private static IActiveViewEvents_ItemAddedEventHandler tocAddHandler;
private static IActiveViewEvents_ItemDeletedEventHandler tocDeleteHandler;
...


protected override void OnStartup()
{
     WireDocumentEvents();     
}
....
 private void WireDocumentEvents()
{
ArcMap.Events.OpenDocument +=
                new ESRI.ArcGIS.ArcMapUI.IDocumentEvents_OpenDocumentEventHandler(PostMxDLoad);

}
...
private void PostMxDLoad()
{
... some other stuff 

 tocDeleteHandler = new IActiveViewEvents_ItemDeletedEventHandler(SettingsMgr.TableofContents.OnTOCItemDeleted);
((IActiveViewEvents_Event)ArcMap.Document.FocusMap).ItemDeleted += tocDeleteHandler;

tocAddHandler = new IActiveViewEvents_ItemAddedEventHandler(SettingsMgr.TableofContents.OnTOCItemAdded);
((IActiveViewEvents_Event)ArcMap.Document.FocusMap).ItemAdded += tocAddHandler;
}
1

You should change your PostMxDLoad method like the following and also add another method, let's say OnActiveViewEventsItemDeleted to handle the event. Here's how:

private void PostMxDLoad()
{
    //... some other stuff 
    if (ArcMap.Document.FocusMap != null)
    {
        //you need to loop the following if there are other dataframes (maps) in your mxd document
        var activeViewEvents = (IActiveViewEvents_Event)ArcMap.Document.FocusMap;
        activeViewEvents.ItemDeleted += new ESRI.ArcGIS.Carto.IActiveViewEvents_ItemDeletedEventHandler(OnActiveViewEventsItemDeleted);
    }
}

private void OnActiveViewEventsItemDeleted(System.Object Item)
{
  // TODO: Add your code here
  System.Windows.Forms.MessageBox.Show("ItemDeleted"); 
}

You can do the same for ItemAdded event accordingly.

| improve this answer | |
  • Thanks for the code snippets. I'm getting "event invocation for COM objects requires event to be attributed with DispIdAttribute" on the creation of the _addinEvents.ItemDeleted creation. Looking into it. – Arnej65 Jan 4 '16 at 16:38
  • 1
    I fixed my issue. the reference to Carto, was added incorrectly. – Arnej65 Jan 4 '16 at 18:45
4

I'm not sure what you want to do exactly, but it looks like you want to receive an event when a layer is added to, or deleted from the map, right? Here's a sample:

private IActiveViewEvents_Event _avEvents;

private void SetupEventHandler() 
{
    var mxDoc = (IMxDocument)ArcMap.Application.Document;
    _avEvents = (IActiveViewEvents_Event)mxDoc.FocusMap;
    _avEvents.ItemAdded += ItemAdded;
    _avEvents.ItemDeleted += ItemDeleted;
}

private void ItemAdded(object item)
{
    System.Windows.Forms.MessageBox.Show("Added");
}

private void ItemDeleted(object item)
{
    System.Windows.Forms.MessageBox.Show("Deleted");
}
| improve this answer | |
  • I'm trying to detect when an item is deleted from the table of contents. Which I don't think code does. Or it is not working for me – Arnej65 Dec 17 '15 at 18:42
  • @Arnej65 My code pops up a messagebox when a layer is deleted from (or added to) the map. It will obviously be removed from the TOC at the same time, but the event handler should be attached to the map. – Berend Dec 21 '15 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.