ArcObjects Library Reference  

EventListener

About the Edit event listener Sample

[C#]

EventListener.cs


using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;

namespace Events
{
  public class EventListener
  {
    #region "Members"
    public event ChangedEventHandler Changed;
    IEngineEditor m_editor;    

    //contains all edit events listed on IEngineEditEvents
    public enum EditorEvent
    {
      OnAbort,
      OnAfterDrawSketch,
      OnBeforeStopEditing,
      OnBeforeStopOperation,
      OnChangeFeature,
      OnConflictsDetected,
      OnCreateFeature, 
      OnCurrentTaskChanged,
      OnCurrentZChanged,
      OnDeleteFeature,
      OnSaveEdits,
      OnSelectionChanged,
      OnSketchFinished,
      OnSketchModified,
      OnStartEditing,
      OnStartOperation,
      OnStopEditing,
      OnStopOperation,
      OnTargetLayerChanged,
      OnVertexAdded,
      OnVertexMoved,
      OnVertexDeleted
    };
    #endregion

    #region "Constructor"
    public EventListener(IEngineEditor editor)
    {
      if (editor == null)
      {
        throw new ArgumentNullException();
      }
      m_editor = editor;
    }
    #endregion

    #region "Event Registration and Handling"
    void OnEvent()
    {
      string eventName = GetEventName();
      UpdateEventList(eventName);
    }

    void OnEvent<T>(T param)
    {
      string eventName = GetEventName();
      UpdateEventList(eventName);
    }   

    public void ListenToEvents(EditorEvent editEvent, bool start)
    {
      switch (editEvent)
      {
        case EditorEvent.OnAbort:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnAbort += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnAbort -= OnEvent;
          break;
        case EditorEvent.OnAfterDrawSketch:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnAfterDrawSketch += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnAfterDrawSketch -= OnEvent;
          break;
        case EditorEvent.OnBeforeStopEditing:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnBeforeStopEditing += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnBeforeStopEditing -= OnEvent;
          break;
        case EditorEvent.OnBeforeStopOperation:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnBeforeStopOperation += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnBeforeStopOperation -= OnEvent;
          break;
        case EditorEvent.OnChangeFeature:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnChangeFeature += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnChangeFeature -= OnEvent;
          break;
        case EditorEvent.OnConflictsDetected:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnConflictsDetected += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnConflictsDetected -= OnEvent;
          break;
        case EditorEvent.OnCreateFeature:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnCreateFeature += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnCreateFeature -= OnEvent;
          break;
        case EditorEvent.OnCurrentTaskChanged:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnCurrentTaskChanged += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnCurrentTaskChanged -= OnEvent;
          break;
        case EditorEvent.OnCurrentZChanged:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnCurrentZChanged += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnCurrentZChanged -= OnEvent;
          break;
        case EditorEvent.OnDeleteFeature:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnDeleteFeature += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnDeleteFeature -= OnEvent;
          break;
        case EditorEvent.OnSaveEdits:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnSaveEdits += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnSaveEdits -= OnEvent;
          break;
        case EditorEvent.OnSelectionChanged:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnSelectionChanged += OnEvent; 
          else
            ((IEngineEditEvents_Event)m_editor).OnSelectionChanged -= OnEvent;
          break;
        case EditorEvent.OnSketchFinished:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnSketchFinished += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnSketchFinished -= OnEvent; 
          break;
        case EditorEvent.OnSketchModified:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnSketchModified += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnSketchModified -= OnEvent;
          break;
        case EditorEvent.OnStartEditing:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnStartEditing += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnStartEditing -= OnEvent; 
          break;
        case EditorEvent.OnStartOperation:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnStartOperation += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnStartOperation -= OnEvent;
          break;
        case EditorEvent.OnStopEditing:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnStopEditing += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnStopEditing -= OnEvent; 
          break;
        case EditorEvent.OnStopOperation:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnStopOperation += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnStopOperation -= OnEvent;
          break;
        case EditorEvent.OnTargetLayerChanged:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnTargetLayerChanged += OnEvent;
          else
            ((IEngineEditEvents_Event)m_editor).OnTargetLayerChanged -= OnEvent;
          break;
        case EditorEvent.OnVertexAdded:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnVertexAdded += OnEvent; 
          else
            ((IEngineEditEvents_Event)m_editor).OnVertexAdded -= OnEvent;
          break;
        case EditorEvent.OnVertexMoved:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnVertexMoved += OnEvent; 
          else
            ((IEngineEditEvents_Event)m_editor).OnVertexMoved -= OnEvent;
          break;
        case EditorEvent.OnVertexDeleted:
          if (start)
            ((IEngineEditEvents_Event)m_editor).OnVertexDeleted += OnEvent;              
          else
            ((IEngineEditEvents_Event)m_editor).OnVertexDeleted -= OnEvent;
          break;
        default:
          throw new ArgumentOutOfRangeException();     
      }

    }
        
    string GetEventName()
    {
      //Get the name of the ArcEngine calling method and use this to indicate the event that was fired
      StackTrace st = new System.Diagnostics.StackTrace();
      StackFrame sf = st.GetFrame(2);
      return (sf.GetMethod().Name);
    }
    void UpdateEventList(string eventName)
    {
      EditorEventArgs e = new EditorEventArgs(eventName);
      if (Changed != null)
        Changed(this, e);
    }
    #endregion
  }

  public class EditorEventArgs : EventArgs
  {
    public EditorEventArgs(string eventType)
    {
      this.eventType = eventType;
    }
    public string eventType;
  }

  public delegate void ChangedEventHandler(object sender, EditorEventArgs e);
}

[Visual Basic .NET]

EventListener.vb

Imports System
Imports System.Diagnostics
Imports System.Collections.Generic
Imports System.Text
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Geodatabase

Namespace Events
  Public Class EventListener
#Region "Members"
    Public Event Changed As ChangedEventHandler
    Dim m_editor As IEngineEditor

    'contains all edit events listed on IEngineEditEvents
    Public Enum EditorEvent
      OnAbort
      OnAfterDrawSketch
      OnBeforeStopEditing
      OnBeforeStopOperation
      OnChangeFeature
      OnConflictsDetected
      OnCreateFeature
      OnCurrentTaskChanged
      OnCurrentZChanged
      OnDeleteFeature
      OnSaveEdits
      OnSelectionChanged
      OnSketchFinished
      OnSketchModified
      OnStartEditing
      OnStartOperation
      OnStopEditing
      OnStopOperation
      OnTargetLayerChanged
      OnVertexAdded
      OnVertexMoved
      OnVertexDeleted
    End Enum

#End Region

#Region "Constructor"
    Public Sub New(ByVal editor As IEngineEditor)
      If (editor Is Nothing) Then
        Throw New ArgumentNullException
      End If
      m_editor = editor
    End Sub
#End Region

#Region "Event Registration and Handling"
    Sub OnEvent()
      Dim eventName As String = GetEventName()
      UpdateEventList(eventName)
    End Sub

    Sub OnEvent(Of T)(ByVal param As T)
      Dim eventName As String = GetEventName()
      UpdateEventList(eventName)
    End Sub

    Public Sub ListenToEvents(ByVal editEvent As EditorEvent, ByVal start As Boolean)
      Select Case editEvent
        Case EditorEvent.OnAbort
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnAbort, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnAbort, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnAfterDrawSketch
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnAfterDrawSketch, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnAfterDrawSketch, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnBeforeStopEditing
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnBeforeStopEditing, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnBeforeStopEditing, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnBeforeStopOperation
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnBeforeStopOperation, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnBeforeStopOperation, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnChangeFeature
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnChangeFeature, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnChangeFeature, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnConflictsDetected
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnConflictsDetected, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnConflictsDetected, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnCreateFeature
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnCreateFeature, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnCreateFeature, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnCurrentTaskChanged
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnCurrentTaskChanged, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnCurrentTaskChanged, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnCurrentZChanged
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnCurrentZChanged, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnCurrentZChanged, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnDeleteFeature
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnDeleteFeature, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnDeleteFeature, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnSaveEdits
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnSaveEdits, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnSaveEdits, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnSelectionChanged
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnSelectionChanged, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnSelectionChanged, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnSketchFinished
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnSketchFinished, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnSketchFinished, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnSketchModified
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnSketchModified, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnSketchModified, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnStartEditing
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnStartEditing, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnStartEditing, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnStartOperation
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnStartOperation, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnStartOperation, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnStopEditing
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnStopEditing, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnStopEditing, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnStopOperation
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnStopOperation, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnStopOperation, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnTargetLayerChanged
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnTargetLayerChanged, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnTargetLayerChanged, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnVertexAdded
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnVertexAdded, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnVertexAdded, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnVertexMoved
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnVertexMoved, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnVertexMoved, AddressOf OnEvent
          End If
          Exit Select
        Case EditorEvent.OnVertexDeleted
          If start Then
            AddHandler DirectCast(m_editor, IEngineEditEvents_Event).OnVertexDeleted, AddressOf OnEvent
          Else
            RemoveHandler DirectCast(m_editor, IEngineEditEvents_Event).OnVertexDeleted, AddressOf OnEvent
          End If
          Exit Select
        Case Else
          Throw New ArgumentOutOfRangeException()
      End Select
    End Sub

    Function GetEventName() As String
      'Get the name of the ArcEngine calling method and use this to indicate the event that was fired
      Dim st As StackTrace = New System.Diagnostics.StackTrace()
      Dim sf As StackFrame = st.GetFrame(2)
      GetEventName = sf.GetMethod().Name
    End Function

    Sub UpdateEventList(ByVal eventName As String)
      Dim e As EditorEventArgs = New EditorEventArgs(eventName)
      RaiseEvent Changed(Me, e)
    End Sub

#End Region
  End Class

  Public Class EditorEventArgs
    Inherits EventArgs
    Public Sub New(ByVal eventType As String)
      Me.eventType = eventType
    End Sub
    Public eventType As String
  End Class

  Public Delegate Sub ChangedEventHandler(ByVal sender As Object, ByVal e As EditorEventArgs)
End Namespace