Form1.vb
Wiring events in ArcGIS Desktop using IActiveViewEvents
Form1.vb
' Copyright 2006 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Public Class Form1

 ' ***** SECTION (Start): Declare Event Handlers
 '
 ' Notes: 
 ' Variables are prefixed with an 'm_' denoting that they are member variables. 
 ' This means they are global in scope for this class.
 '
 ' The different IActiveViewEvents_XXXXXXXXX are 'hidden types' and do not show 
 ' up using intellisense in VB.NET of Visaul Studio 2005. You can see them 
 ' however using the Object Browser.
 Private m_ActiveViewEventsAfterDraw As ESRI.ArcGIS.Carto.IActiveViewEvents_AfterDrawEventHandler
 Private m_ActiveViewEventsAfterItemDraw As ESRI.ArcGIS.Carto.IActiveViewEvents_AfterItemDrawEventHandler
 Private m_ActiveViewEventsContentsChanged As ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsChangedEventHandler
 Private m_ActiveViewEventsContentsCleared As ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsClearedEventHandler
 Private m_ActiveViewEventsFocusMapChanged As ESRI.ArcGIS.Carto.IActiveViewEvents_FocusMapChangedEventHandler
 Private m_ActiveViewEventsItemAdded As ESRI.ArcGIS.Carto.IActiveViewEvents_ItemAddedEventHandler
 Private m_ActiveViewEventsItemDeleted As ESRI.ArcGIS.Carto.IActiveViewEvents_ItemDeletedEventHandler
 Private m_ActiveViewEventsItemReordered As ESRI.ArcGIS.Carto.IActiveViewEvents_ItemReorderedEventHandler
 Private m_ActiveViewEventsSelectionChanged As ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler
 Private m_ActiveViewEventsSpatialReferenceChanged As ESRI.ArcGIS.Carto.IActiveViewEvents_SpatialReferenceChangedEventHandler
 Private m_ActiveViewEventsViewRefreshed As ESRI.ArcGIS.Carto.IActiveViewEvents_ViewRefreshedEventHandler
 ' ***** SECTION (End): Declare Event Handlers

 Private Sub btnStep1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStep1.Click

  ' map is set by referencing the public shared member variable (m_map) in the 
  ' CommandWiringEvents class. This is one way to pass variables between classes in an application.
  Dim map As ESRI.ArcGIS.Carto.IMap = CommandWiringEvents.m_map

  TextBox3.Clear()

  ' ***** SECTION (Start): Set up the event handlers for all of the IActiveViewEvents
  '
  ' Notes:
  ' You set the member IActiveViewEvents_XXXXXXXXX variables to the EventHandler
  ' for the specific events you want to capture. VB.NET uses the AddressOf 
  ' operator to emulate 'C++' pointer like functionality.

  ' Create an instance of the delegate, add it to AfterDraw event
  m_ActiveViewEventsAfterDraw = New ESRI.ArcGIS.Carto.IActiveViewEvents_AfterDrawEventHandler(AddressOf OnActiveViewEventsAfterDraw)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).AfterDraw, m_ActiveViewEventsAfterDraw

  ' Create an instance of the delegate, add it to AfterItemDraw event
  m_ActiveViewEventsAfterItemDraw = New ESRI.ArcGIS.Carto.IActiveViewEvents_AfterItemDrawEventHandler(AddressOf OnActiveViewEventsItemDraw)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).AfterItemDraw, m_ActiveViewEventsAfterItemDraw

  ' Create an instance of the delegate, add it to ContentsChanged event
  m_ActiveViewEventsContentsChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsChangedEventHandler(AddressOf OnActiveViewEventsContentsChanged)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ContentsChanged, m_ActiveViewEventsContentsChanged

  ' Create an instance of the delegate, add it to ContentsCleared event
  m_ActiveViewEventsContentsCleared = New ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsClearedEventHandler(AddressOf OnActiveViewEventsContentsCleared)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ContentsCleared, m_ActiveViewEventsContentsCleared

  ' Create an instance of the delegate, add it to FocusMapChanged event
  m_ActiveViewEventsFocusMapChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_FocusMapChangedEventHandler(AddressOf OnActiveViewEventsFocusMapChanged)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).FocusMapChanged, m_ActiveViewEventsFocusMapChanged

  ' Create an instance of the delegate, add it to ItemAdded event
  m_ActiveViewEventsItemAdded = New ESRI.ArcGIS.Carto.IActiveViewEvents_ItemAddedEventHandler(AddressOf OnActiveViewEventsItemAdded)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemAdded, m_ActiveViewEventsItemAdded

  ' Create an instance of the delegate, add it to ItemDeleted event
  m_ActiveViewEventsItemDeleted = New ESRI.ArcGIS.Carto.IActiveViewEvents_ItemDeletedEventHandler(AddressOf OnActiveViewEventsItemDeleted)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemDeleted, m_ActiveViewEventsItemDeleted

  ' Create an instance of the delegate, add it to ItemReordered event
  m_ActiveViewEventsItemReordered = New ESRI.ArcGIS.Carto.IActiveViewEvents_ItemReorderedEventHandler(AddressOf OnActiveViewEventsItemReordered)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemReordered, m_ActiveViewEventsItemReordered

  ' Create an instance of the delegate, add it to SelectionChanged event
  m_ActiveViewEventsSelectionChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler(AddressOf OnActiveViewEventsSelectionChanged)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SelectionChanged, m_ActiveViewEventsSelectionChanged

  ' Create an instance of the delegate, add it to SpatialReferenceChanged event
  m_ActiveViewEventsSpatialReferenceChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_SpatialReferenceChangedEventHandler(AddressOf OnActiveViewEventsSpatialReferenceChanged)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SpatialReferenceChanged, m_ActiveViewEventsSpatialReferenceChanged

  ' Create an instance of the delegate, add it to ViewRefreshed event
  m_ActiveViewEventsViewRefreshed = New ESRI.ArcGIS.Carto.IActiveViewEvents_ViewRefreshedEventHandler(AddressOf OnActiveViewEventsViewRefreshed)
  AddHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ViewRefreshed, m_ActiveViewEventsViewRefreshed
  ' ***** SECTION (End): Set up the event handlers for all of the IActiveViewEvents

 End Sub

 Private Sub btnStep3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStep3.Click

  ' map is set by referencing the public shared member variable (m_map) in the 
  ' CommandWiringEvents class. This is one way to pass variables between classes in an  application.
  Dim map As ESRI.ArcGIS.Carto.IMap = CommandWiringEvents.m_map

  ' ***** SECTION (Start): Remove Event Handlers
  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).AfterDraw, m_ActiveViewEventsAfterDraw

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).AfterItemDraw, m_ActiveViewEventsAfterItemDraw

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ContentsChanged, m_ActiveViewEventsContentsChanged

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ContentsCleared, m_ActiveViewEventsContentsCleared

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).FocusMapChanged, m_ActiveViewEventsFocusMapChanged

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemAdded, m_ActiveViewEventsItemAdded

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemDeleted, m_ActiveViewEventsItemDeleted

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ItemReordered, m_ActiveViewEventsItemReordered

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SelectionChanged, m_ActiveViewEventsSelectionChanged

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).SpatialReferenceChanged, m_ActiveViewEventsSpatialReferenceChanged

  RemoveHandler CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event).ViewRefreshed, m_ActiveViewEventsViewRefreshed
  ' ***** SECTION (End): Remove Event Handlers

 End Sub

 ' ***** SECTION (Start): Custom Functions that you write to add additionaly functionality for the events
 ' Event handler
 Private Sub OnActiveViewEventsAfterDraw(ByVal Display As ESRI.ArcGIS.Display.IDisplay, ByVal phase As ESRI.ArcGIS.Carto.esriViewDrawPhase)
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("AfterDraw")
  TextBox3.Text = TextBox3.Text + vbCrLf + "AfterDraw"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsItemDraw(ByVal Index As Short, ByVal Display As ESRI.ArcGIS.Display.IDisplay, ByVal phase As ESRI.ArcGIS.esriSystem.esriDrawPhase)
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("ItemDraw")
  TextBox3.Text = TextBox3.Text + vbCrLf + "ItemDraw"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsContentsChanged()
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("ContentsChanged")
  TextBox3.Text = TextBox3.Text + vbCrLf + "ContentsChanged"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsContentsCleared()
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("ContentsCleared")
  TextBox3.Text = TextBox3.Text + vbCrLf + "ContentsCleared"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsFocusMapChanged()
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("FocusMapChanged")
  TextBox3.Text = TextBox3.Text + vbCrLf + "FocusMapChanged"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsItemAdded(ByVal Item As Object)
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("ItemAdded")
  TextBox3.Text = TextBox3.Text + vbCrLf + "ItemAdded"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsItemDeleted(ByVal Item As Object)
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("ItemDeleted")
  TextBox3.Text = TextBox3.Text + vbCrLf + "ItemDeleted"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsItemReordered(ByVal Item As Object, ByVal toIndex As Integer)
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("ItemReordered")
  TextBox3.Text = TextBox3.Text + vbCrLf + "ItemReordered"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsSelectionChanged()
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("SelectionChanged")
  TextBox3.Text = TextBox3.Text + vbCrLf + "SelectionChanged"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsSpatialReferenceChanged()
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("SpatialReferenceChanged")
  TextBox3.Text = TextBox3.Text + vbCrLf + "SpatialReferenceChanged"
 End Sub

 ' Event handler
 Private Sub OnActiveViewEventsViewRefreshed(ByVal view As ESRI.ArcGIS.Carto.IActiveView, ByVal phase As ESRI.ArcGIS.Carto.esriViewDrawPhase, ByVal data As Object, ByVal envelope As ESRI.ArcGIS.Geometry.IEnvelope)
  ' Add your code here
  ' System.Windows.Forms.MessageBox.Show("ViewRefreshed")
  TextBox3.Text = TextBox3.Text + vbCrLf + "ViewRefreshed"
 End Sub
 ' ***** SECTION (End): Custom Functions that you write to add additionaly functionality for the events

End Class