listen to event add/remove layer

4202
10
06-13-2011 09:35 AM
JamesWhisenhunt
New Contributor III
Hello all,

I would like to listen for the event add/remove layers in the TOC.  I can not seem to find this anywhere.  I am able to listen for several events; however, the add/remove layer eludes me. 

Any help is greatly appreciated. 

James
0 Kudos
10 Replies
JeffreyHamblin
New Contributor III
I was looking for something similar and found:
IMapAdmin3
- FireLayerAdded
- FireLayerDeleted

Map (IMap) implements the interface.

I have not yet worked on hooking them, but maybe this will get you started.

(Edit) Thanks go to subsequent posters for pointing out that the above methods trigger
events, and where to find the correct events to hook.


-Jeff
0 Kudos
StanGrochowski
New Contributor
I too have been having this problem.  In 9.3.1 I could use IActiveViewEvents_ItemAddedEventHandler(OnItemAdded) and IActiveViewEvents_ItemDeletedEventHandler(OnItemDeleted) without a problem but now in 10 SP2 it never fires that event.  I have not been able to find a way to listen to add/remove layer.
0 Kudos
AlexanderGray
Occasional Contributor III
MapAdmin has methods to fire that in turn triggers the layer added event, I don't think this is what you are looking for.  A MapClass implements IActiveViewEvents which has itemAdded, ItemDeleted etc that will fire when a layer is added or removed.
0 Kudos
AlexanderGray
Occasional Contributor III
Just saw the update, I am still at SP1, I will check this out SP2.  It is very relevant to my application.
0 Kudos
JamesWhisenhunt
New Contributor III
After tinkering with it for a while I was able to get the IActiveViewEvents_ItemAddedEventHandler(OnItemAdded) to work.  I would try the service packs to see if it helps. 

I appreciate all the advise.

James
0 Kudos
StanGrochowski
New Contributor
I am using SP2 and not able to get it to work.  What was the tinkering you were doing which got it to work. 

My code is below:  This same code works without a problem in 9.3.1


        private IActiveViewEvents_ItemAddedEventHandler m_ActiveViewEventsItemAdded;
        private IActiveViewEvents_ItemDeletedEventHandler m_ActiveViewEventsItemDeleted;

public void OnCreate(object hook)
{
            m_app = hook as IApplication;
            m_MxDoc = m_app.Document as IMxDocument;
            m_Map = m_MxDoc.FocusMap as Map;

            //Create an instance of the delegate, add it to ItemAdded event
            m_ActiveViewEventsItemAdded = new IActiveViewEvents_ItemAddedEventHandler(OnActiveViewEventsItemAdded);
            ((IActiveViewEvents_Event)(m_Map)).ItemAdded += m_ActiveViewEventsItemAdded;

            //Create an instance of the delegate, add it to ItemDeleted event
            m_ActiveViewEventsItemDeleted = new IActiveViewEvents_ItemDeletedEventHandler(OnActiveViewEventsItemDeleted);
            ((IActiveViewEvents_Event)(m_Map)).ItemDeleted += m_ActiveViewEventsItemDeleted;
        }

public void OnActiveViewEventsItemAdded(object Item)
        {
            string name;
            if (Item is IGroupLayer)  {
               // some code here
               }
          }
0 Kudos
JamesWhisenhunt
New Contributor III
I am using VB.Net.  I believe the C# and VB handles this different. 

Here is my VB code.  I really hope this helps.

James


    Private m_application As ESRI.ArcGIS.ArcMap.Application
    Private m_pMap As IMap
    Private m_pActiveView As IActiveView
    Private m_pMxDoc As IMxDocument

    Dim m_docEvents As IDocumentEvents_Event
    Dim activeViewEvents As IActiveViewEvents_Event

    Public Sub New()

        m_application = My.ArcMap.ThisApplication
        m_pMap = Mod1.GetMapFromArcMap(m_application)
        m_pMxDoc = Mod1.GetMxDocumentFromArcMap(m_application)
        m_pActiveView = Mod1.GetActiveViewFromArcMap(m_application)


        activeViewEvents = TryCast(m_pMap, IActiveViewEvents_Event)
        AddHandler activeViewEvents.ItemAdded, AddressOf LayersAdded
        AddHandler activeViewEvents.ItemDeleted, AddressOf LayersDelete
        AddHandler activeViewEvents.ContentsChanged, AddressOf TOCChanged

        m_docEvents = CType(m_pMxDoc, IDocumentEvents_Event)
        AddHandler m_docEvents.NewDocument, AddressOf OnNewDoc
        AddHandler m_docEvents.OpenDocument, AddressOf OnOpenDoc
        AddHandler m_docEvents.ActiveViewChanged, AddressOf OnActiveViewChange

    End Sub


    Sub LayersAdded()
        MsgBox("LayersAdded")
    End Sub
    Sub LayersDelete()
        MsgBox("LayersDelete")
    End Sub
    Sub TOCChanged()
        MsgBox("TOCChanged")
    End Sub

    Sub OnNewDoc()
        MsgBox("OnNewDoc")
    End Sub
    Sub OnOpenDoc()
        MsgBox(" OnOpenDoc")
    End Sub
    Sub OnActiveViewChange()
        MsgBox("OnActiveViewChange")
    End Sub
0 Kudos
StanGrochowski
New Contributor
I think I finally found the source of the problem I was having with the IActiveViewEvents listener.  It seems that the listener works fine if I am using the default mxd.  If I open a saved mxd file and add or delete a layer the event handlers never fire.  I haven't found a workaround for this problem.
0 Kudos
AlexanderGray
Occasional Contributor III
That is because the activeview listeners are bound to a specific map or page layout.  You can have multiple maps and one layout in a document with listeners on some of them but not others.  If you open a new map you get a brand new set of maps and a layout with no listeners on them yet.  You need to have listeners on the document events newDocument, OpenDocument and closeDocument.  The new and open to set up your listeners on the appropriate maps and the closedocument to remove the listeners.
Good luck.
0 Kudos