ArcObjects Library Reference

Add Event Wiring for New and Open Documents Snippet

Wire event when a document is created or opened in ArcGIS desktop applications.

[C#]

/// <summary>
/// Event member variable
/// </summary>
/// <remarks></remarks>
private ESRI.ArcGIS.ArcMapUI.IDocumentEvents_Event m_docEvents = null;  
 
/// <summary>
/// Set up the wiring of the events.
/// </summary>
/// <param name="myDocument"></param>
/// <remarks></remarks>
private void SetUpDocumentEvent(ESRI.ArcGIS.Framework.IDocument myDocument)
{
  m_docEvents = myDocument as ESRI.ArcGIS.ArcMapUI.IDocumentEvents_Event;  
  m_docEvents.NewDocument += new ESRI.ArcGIS.ArcMapUI.IDocumentEvents_NewDocumentEventHandler(OnNewDocument);
  m_docEvents.OpenDocument += new ESRI.ArcGIS.ArcMapUI.IDocumentEvents_OpenDocumentEventHandler(OnOpenDocument);
}
 
/// <summary>
/// The NewDocument event handler. 
/// </summary>
/// <remarks></remarks>
void OnNewDocument()
{
  ESRI.ArcGIS.Framework.IDocument theDocument = m_docEvents as ESRI.ArcGIS.Framework.IDocument;
  //TODO: Add your document handler event code
}

/// <summary>
/// The OpenDocument event handler.
/// </summary>
/// <remarks></remarks>
void OnOpenDocument()
{
  ESRI.ArcGIS.Framework.IDocument theDocument = m_docEvents as ESRI.ArcGIS.Framework.IDocument; 
 //TODO: Add your document handler event code
}
[Visual Basic .NET]

''' <summary>
''' Event member variable
''' </summary>
''' <remarks></remarks>
Private m_docEvents As ESRI.ArcGIS.ArcMapUI.IDocumentEvents_Event
 
''' <summary>
''' Set up the wiring of the events.
''' </summary>
''' <param name="myDocument"></param>
''' <remarks></remarks>
Private Sub SetUpDocumentEvent(myDocument As ESRI.ArcGIS.Framework.IDocument)
    m_docEvents = CType(myDocument, ESRI.ArcGIS.ArcMapUI.IDocumentEvents_Event)
    AddHandler m_docEvents.NewDocument, AddressOf OnNewDocument
    AddHandler m_docEvents.OpenDocument, AddressOf OnOpenDocument
End Sub
 
''' <summary>
''' The NewDocument event handler. 
''' </summary>
''' <remarks></remarks>
Private Sub OnNewDocument()
    Dim theDocument As ESRI.ArcGIS.Framework.IDocument = CType(m_docEvents, ESRI.ArcGIS.Framework.IDocument) 
    'TODO: Add your document handler event code
End Sub

''' <summary>
''' The OpenDocument event handler.
''' </summary>
''' <remarks></remarks>
Private Sub OnOpenDocument()
    Dim theDocument As ESRI.ArcGIS.Framework.IDocument = CType(m_docEvents, ESRI.ArcGIS.Framework.IDocument) 
    'TODO: Add your document handler event code
End Sub


Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.ArcMapUI
  • ESRI.ArcGIS.Framework