Hi Rob
Yes, I found all that behavior a bit challenging too.
I set up document and ActiveView Events in an extension. I found that I had to setup the document events only once. I removed the events on extension shut down.
On the other hand, each time the document.ActiveViewChanged event is fired I remove my old events then add the new ones, see below.
Hope this helps.
Private Sub OnActiveViewChanged()
Try
Dim pActiveView As IActiveView = m_pMxDocument.ActiveView
Dim pMap As IMap
Dim pPageLayout As IPageLayout
If TypeOf pActiveView Is IMap Then
pMap = pActiveView.FocusMap
m_pActiveViewEvents = CType(pMap, IActiveViewEvents_Event)
Debug.Print("Active View is IMAP, FocusMap is: " & pActiveView.FocusMap.Name)
ElseIf TypeOf pActiveView Is IPageLayout Then
pPageLayout = m_pMxDocument.PageLayout
m_pActiveViewEvents = CType(pPageLayout, IActiveViewEvents_Event)
Debug.Print("Active View is IPageLayout, FocusMap is: " & pActiveView.FocusMap.Name)
Else
Return
End If
Me.RemoveActiveViewEvents()
Me.AddActiveViewEvents()
Catch ex As Exception
DebugMsgBox("OnActiveviewChanged() Error: " & ex.ToString)
End Try
End Sub
Public Sub AddActiveViewEvents()
Try
If m_pActiveViewEvents IsNot Nothing Then
AddHandler m_pActiveViewEvents.FocusMapChanged, AddressOf OnActiveViewEventsFocusMapChanged
AddHandler m_pActiveViewEvents.ItemAdded, AddressOf OnActiveViewEventsItemAdded
AddHandler m_pActiveViewEvents.ItemDeleted, AddressOf OnActiveViewEventsItemDeleted
DebugMsgBox("AddActiveViewEvents")
Else
DebugMsgBox("AddActiveViewEvents m_pActiveViewEvents = NOTHING")
End If
Catch ex As Exception
End Try
End Sub
Public Sub RemoveActiveViewEvents()
Try
If m_pActiveViewEvents IsNot Nothing Then
RemoveHandler m_pActiveViewEvents.FocusMapChanged, AddressOf OnActiveViewEventsFocusMapChanged
RemoveHandler m_pActiveViewEvents.ItemAdded, AddressOf OnActiveViewEventsItemAdded
RemoveHandler m_pActiveViewEvents.ItemDeleted, AddressOf OnActiveViewEventsItemDeleted
DebugMsgBox("RemoveActiveViewEvents")
Else
DebugMsgBox("RemoveActiveViewEvents - m_pActiveViewEvents = NOTHING")
End If
Catch ex As Exception
End Try
End Sub