2

Normally, in ArcMap, when an event handler to catch the FocusMapChanged event is set up, and a new map (data frame) is activated, when the event fires, a reference to the FocusMap returns the map that is being activated. BUT, when the currently active map is removed from the document, when the event fires, a reference to the FocusMap returns the map that is being removed! What?! This behaviour is not helpful to me.

Here's code that displays a message box with the name of the Focus Map whenever the FocusMapChanged event fires...

Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.ArcMapUI

Public Class test_FocusMapChanged
    Inherits ESRI.ArcGIS.Desktop.AddIns.Button
    Private m_FocusMapChanged
    Private activeViewEventsPL As IActiveViewEvents_Event

    Private Sub ActiveViewEventsHandler(ByVal map As IMxDocument)
        activeViewEventsPL = TryCast(map.PageLayout, IActiveViewEvents_Event)

        m_FocusMapChanged = New IActiveViewEvents_FocusMapChangedEventHandler(AddressOf FocusMapChanged)
        AddHandler activeViewEventsPL.FocusMapChanged, m_FocusMapChanged
    End Sub
    Private Sub FocusMapChanged()
        MsgBox(My.ArcMap.Document.ActiveView.FocusMap.Name)
    End Sub

    Protected Overrides Sub OnClick()
        ActiveViewEventsHandler(My.ArcMap.Document)
    End Sub

Does anyone have any ideas of how to get around this and get a reference to the newly activated map rather than the removed map?

  • There should be two events fired: activate the map being removed so it can be removed, then there should be no active map... perhaps the 2nd event is bailing on Null Reference My.ArcMap.Document.ActiveView.FocusMap == null; – Michael Stimson May 4 '15 at 22:33
1

So I ended up contacting Esri support about this... This can be worked around by wiring up the ItemDeleted event to the PageLayout. Then the ItemDeleted event will be fired whenever a map is deleted.

This gets me what I want:

Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.ArcMapUI

Public Class test_FocusMapChanged
    Inherits ESRI.ArcGIS.Desktop.AddIns.Button
    Private m_ItemDeleted
    Private m_FocusMapChanged
    Private activeViewEventsPL As IActiveViewEvents_Event
    Shared theDataFrame

    Private Sub ActiveViewEventsHandler(ByVal map As IMxDocument)
        activeViewEventsPL = TryCast(map.PageLayout, IActiveViewEvents_Event)

        m_FocusMapChanged = New IActiveViewEvents_FocusMapChangedEventHandler(AddressOf FocusMapChanged)
        AddHandler activeViewEventsPL.FocusMapChanged, m_FocusMapChanged

        m_ItemDeleted = New IActiveViewEvents_ItemDeletedEventHandler(AddressOf ItemDeleted)
        AddHandler activeViewEventsPL.ItemDeleted, m_ItemDeleted
    End Sub
    Private Sub FocusMapChanged()
        theDataFrame = My.ArcMap.Document.FocusMap
    End Sub
    Private Sub ItemDeleted(ByVal item As Object)
        Dim pMapFrame As IMapFrame = item
        If pMapFrame.Map Is theDataFrame Then
            MsgBox(My.ArcMap.Document.FocusMap.Name)
        Else

        End If
    End Sub

    Protected Overrides Sub OnClick()
        ActiveViewEventsHandler(My.ArcMap.Document)
    End Sub
End Class

I had to keep track of the map when the FocusMapChanged event occurs because if the active map is deleted the FocusMapChanged event fires first, and then the map is removed; so if the active map when the FocusMapChanged event fires is the same as the map being removed in the ItemDeleted event, then I want my code to run.

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.