1

Is there an event in ArcObjects to determine whether a layer (specifically a shapefile) has been loaded into a map?

I'm using ArcObjects 10.4.1 and C#

4

The event handler for capturing layers added to the map is IActiveViewEvents.ItemAdded

Make sure its pointing to a Map and not PageLayout.

You would then test to see if it is specifically a shapefile.

|improve this answer|||||
  • That's what I was looking for. Thanks. – Jose Jul 2 '19 at 21:31
  • The object of capturing this event is to rebuild feature cache. My code is now capturing properly both ItemDeleted and ItemAdded events. Now with the later, i'm losing feature cache even though I'm calling the same method for both events. Is there another event handler being called after ItemAdded that might be forcing to lose feature cache again? – Jose Jul 3 '19 at 9:09
  • No idea and without seeing the code I can't really comment, are you talking about this form of cache? – Hornbydd Jul 3 '19 at 9:14
  • I mean link To rebuild cache I'm using BuildCache method. I can post code if it does help. – Jose Jul 3 '19 at 9:20
  • In the remarks section of the link you supplied it explicitly states caches are not supported for shapefiles, may be that is your issue? – Hornbydd Jul 3 '19 at 9:41
3

Here is some boiler plate code to set up active view events. You'll just need to write a function that checks if the item added is a shapefile. Call that function from Events_ItemAdded().

    public static IActiveViewEvents_Event ActiveViewEvent;    

    private void WireDocumentEvents()
    {
        // Event handlers
        ArcMap.Events.CloseDocument += new ESRI.ArcGIS.ArcMapUI.IDocumentEvents_CloseDocumentEventHandler(Events_CloseDocument);

        if (ActiveViewEvent == null)
            ActiveViewEvent = ArcMap.Document.FocusMap as IActiveViewEvents_Event;

        if (ActiveViewEvent == null) return;

        ActiveViewEvent.ItemAdded += Events_ItemAdded;
        ActiveViewEvent.ItemDeleted += Events_ItemRemoved;
    }

    private void UnWireDocumentEvents()
    {
        // Unload Event handlers
        ArcMap.Events.CloseDocument -= new ESRI.ArcGIS.ArcMapUI.IDocumentEvents_CloseDocumentEventHandler(Events_CloseDocument);

        if (ActiveViewEvent == null) return;

        ActiveViewEvent.ItemAdded -= Events_ItemAdded;
        ActiveViewEvent.ItemDeleted -= Events_ItemRemoved;

        ActiveViewEvent = null;
    }

    void Events_CloseDocument()
    {
        //DO SOMETHING WHEN DOCUMENT CLOSES
    }

    private void Events_ItemAdded(object item)
    {
       //DO SOMETHING WHEN ITEM ADDED
    }

    private void Events_ItemRemoved(object item)
    {
        //DO SOMETHING WHEN ITEM REMOVED
    }
|improve this answer|||||
  • Thank you for the heads up code... +1 from me – Jose Jul 2 '19 at 21:31
  • I think more code might be needed to handle when dataframes are added, removed, switched. – Kirk Kuykendall Jan 30 at 17:30

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.