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
Solved! Go to Solution.
Private m_documentEvents As IDocumentEvents_Event Private m_activeViewEvents As IActiveViewEvents_Event
Dim mxDocument As IMxDocument = DirectCast(ESRIContainer.Instance.Application.Document, IMxDocument) m_documentEvents = DirectCast(mxDocument, IDocumentEvents_Event) m_activeViewEvents = DirectCast(mxDocument.FocusMap, IActiveViewEvents_Event) AddHandler m_documentEvents.NewDocument, AddressOf NewDocument AddHandler m_documentEvents.OpenDocument, AddressOf OpenDocument AddHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded AddHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted
Private Sub NewDocument() RemoveHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded RemoveHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted Dim mxDocument As IMxDocument = DirectCast(ESRIContainer.Instance.Application.Document, IMxDocument) m_activeViewEvents = DirectCast(mxDocument.FocusMap, IActiveViewEvents_Event) AddHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded AddHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted LoadOutputMapItems() End Sub Private Sub OpenDocument() RemoveHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded RemoveHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted Dim mxDocument As IMxDocument = DirectCast(ESRIContainer.Instance.Application.Document, IMxDocument) m_activeViewEvents = DirectCast(mxDocument.FocusMap, IActiveViewEvents_Event) AddHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded AddHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted LoadOutputMapItems() End Sub
Private m_documentEvents As IDocumentEvents_Event Private m_activeViewEvents As IActiveViewEvents_Event
Dim mxDocument As IMxDocument = DirectCast(ESRIContainer.Instance.Application.Document, IMxDocument) m_documentEvents = DirectCast(mxDocument, IDocumentEvents_Event) m_activeViewEvents = DirectCast(mxDocument.FocusMap, IActiveViewEvents_Event) AddHandler m_documentEvents.NewDocument, AddressOf NewDocument AddHandler m_documentEvents.OpenDocument, AddressOf OpenDocument AddHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded AddHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted
Private Sub NewDocument() RemoveHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded RemoveHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted Dim mxDocument As IMxDocument = DirectCast(ESRIContainer.Instance.Application.Document, IMxDocument) m_activeViewEvents = DirectCast(mxDocument.FocusMap, IActiveViewEvents_Event) AddHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded AddHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted LoadOutputMapItems() End Sub Private Sub OpenDocument() RemoveHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded RemoveHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted Dim mxDocument As IMxDocument = DirectCast(ESRIContainer.Instance.Application.Document, IMxDocument) m_activeViewEvents = DirectCast(mxDocument.FocusMap, IActiveViewEvents_Event) AddHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded AddHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted LoadOutputMapItems() End Sub