AnsweredAssumed Answered

IActiveViewEvents SelectionChanged stops working after IDocumentEvents OpenDocument

Question asked by keitheweber on Apr 1, 2014
Latest reply on Apr 2, 2014 by keitheweber
I am developing an extension to ArcMap and trying to listen to the SelectionChanged event. There are several classes in my code that want to be notified when the selection changes so I was trying to write a simple wrapper to handle setting up the event handlers. The code works when ArcMap starts up and for the NewDocument event, but as soon as the OpenDocument event occurs, nothing works until the application is restarted. No exceptions (that I can tell) are thrown through the entire process. Any help would be greatly appreciated.

Update: I also noticed that after the OpenDocument event occurs, All of the IDocumentEvents stop working.



Imports ESRI.ArcGIS.ArcMapUI Imports ESRI.ArcGIS.Carto Imports ESRI.ArcGIS.Framework Imports System.Windows.Forms  Public Class MapEvents     Implements IDisposable      Private Property _application As IApplication     Private Property _activeViewListening As Boolean      Public Event ActiveViewSelectionChanged()     Public Event ActiveViewItemDeleted(ByRef Item As Object)      Public Sub New(ByRef application As IApplication)         _application = application         Dim mxDocument As IMxDocument = application.Document         StartListening(mxDocument)         _activeViewListening = False     End Sub      Public Sub Dispose() Implements IDisposable.Dispose         Dim mxDocument As IMxDocument = _application.Document         StopListening(mxDocument)         StopListening(mxDocument.ActiveView)         _application = Nothing     End Sub       Private Sub StartListening(ByRef mxDocument As IMxDocument)         If (mxDocument Is Nothing) Then             Return         End If         Dim documentEvents As IDocumentEvents_Event = mxDocument         AddHandler documentEvents.NewDocument, AddressOf OnNewDocument         AddHandler documentEvents.OpenDocument, AddressOf OnOpenDocument         AddHandler documentEvents.BeforeCloseDocument, AddressOf OnBeforeCloseDocument     End Sub      Private Sub StopListening(ByRef mxDocument As IMxDocument)         If (mxDocument Is Nothing) Then             Return         End If         Dim documentEvents As IDocumentEvents_Event = mxDocument         RemoveHandler documentEvents.NewDocument, AddressOf OnNewDocument         RemoveHandler documentEvents.OpenDocument, AddressOf OnOpenDocument         RemoveHandler documentEvents.BeforeCloseDocument, AddressOf OnBeforeCloseDocument     End Sub      Private Sub StartListening(ByRef activeView As IActiveView)         If (_activeViewListening) Then             Return         End If         If (activeView Is Nothing) Then             Return         End If         Dim activeViewEvents As IActiveViewEvents_Event = activeView         AddHandler activeViewEvents.SelectionChanged, AddressOf OnActiveViewSelectionChanged         AddHandler activeViewEvents.ItemDeleted, AddressOf OnActiveViewItemDeleted         _activeViewListening = True         MessageBox.Show("Active View Listening = True")     End Sub      Private Sub StopListening(ByRef activeView As IActiveView)         If (activeView Is Nothing) Then             Return         End If         Dim activeViewEvents As IActiveViewEvents_Event = activeView         RemoveHandler activeViewEvents.SelectionChanged, AddressOf OnActiveViewSelectionChanged         RemoveHandler activeViewEvents.ItemDeleted, AddressOf OnActiveViewItemDeleted         _activeViewListening = False         MessageBox.Show("Active View Listening = False")     End Sub      Private Sub StartActiveViewListening()         Dim mxDocument As IMxDocument = _application.Document         StartListening(mxDocument.ActiveView)     End Sub      Private Sub OnNewDocument()         Try             StartActiveViewListening()         Catch ex As Exception             Dim errorForm As ErrorForm = New ErrorForm(ex)             errorForm.Show()         End Try     End Sub      Private Sub OnOpenDocument()         Try             StartActiveViewListening()         Catch ex As Exception             Dim errorForm As ErrorForm = New ErrorForm(ex)             errorForm.Show()         End Try     End Sub       Private Sub OnActiveViewSelectionChanged()         Try             MessageBox.Show("Active View Selection Changed.")             RaiseEvent ActiveViewSelectionChanged()         Catch ex As Exception             Dim errorForm As ErrorForm = New ErrorForm(ex)             errorForm.Show()         End Try     End Sub      Private Sub OnActiveViewItemDeleted(Item As Object)         Try             MessageBox.Show("Active View Item Deleted.")             RaiseEvent ActiveViewItemDeleted(Item)         Catch ex As Exception             Dim errorForm As ErrorForm = New ErrorForm(ex)             errorForm.Show()         End Try     End Sub      Private Function OnBeforeCloseDocument() As Boolean         Try             Dim mxDocument As IMxDocument = _application.Document             StopListening(mxDocument.ActiveView)         Catch ex As Exception             Dim errorForm As ErrorForm = New ErrorForm(ex)             errorForm.Show()         End Try         Return False     End Function   End Class

Outcomes