Document object events are used to respond to the document window. When a document event is registered, it is only associated with the document object in which it is associated. So if an event needs to be registered with each document, you will want to use the DocumentCreated event of the DocumentCollection object to register events with each new or opened drawing.
The following events are available for Document objects:
Triggered just after a request is received to close a drawing.
Triggered when a drawing is about to be opened.
Triggered when an attempt to close a drawing is aborted.
Triggered after the BeginDocumentClose event and before closing the drawing begins.
Triggered when a command is cancelled before it completes.
Triggered immediately after a command completes.
Triggered when a command fails to complete and is not cancelled.
Triggered immediately after a command is issued, but before it completes.
Triggered when a drawing has been opened.
Triggered when the current pickfirst selection set changes.
Triggered after a layout has been set current.
Triggered after a layout is being set current.
Triggered when the evaluation of a LISP expression is canceled.
Triggered upon completion of evaluating a LISP expression.
Triggered immediately after AutoCAD receives a request to evaluate a LISP expression.
Triggered immediately when an unknown command is entered at the Command prompt.
Triggered after the view of a drawing has changed.
The following example uses the BeginDocumentClose event to prompt the user if they want to continue closing the current drawing. A message box is displayed with the Yes and No buttons. Clicking No aborts the closing of the drawing by using the Veto method of the arguments that are returned by the event handler.
Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.ApplicationServices <CommandMethod("AddDocEvent")> _ Public Sub AddDocEvent() '' Get the current document Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument AddHandler acDoc.BeginDocumentClose, AddressOf docBeginDocClose End Sub <CommandMethod("RemoveDocEvent")> _ Public Sub RemoveDocEvent() '' Get the current document Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument RemoveHandler acDoc.BeginDocumentClose, AddressOf docBeginDocClose End Sub Public Sub docBeginDocClose(ByVal senderObj As Object, _ ByVal docBegClsEvtArgs As DocumentBeginCloseEventArgs) '' Display a message box prompting to continue closing the document If System.Windows.Forms.MessageBox.Show( _ "The document is about to be closed." & _ vbLf & "Do you want to continue?", _ "Close Document", _ System.Windows.Forms.MessageBoxButtons.YesNo) = _ System.Windows.Forms.DialogResult.No Then docBegClsEvtArgs.Veto() End If End If
using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; [CommandMethod("AddDocEvent")] public void AddDocEvent() { // Get the current document Document acDoc = Application.DocumentManager.MdiActiveDocument; acDoc.BeginDocumentClose += new DocumentBeginCloseEventHandler(docBeginDocClose); } [CommandMethod("RemoveDocEvent")] public void RemoveDocEvent() { // Get the current document Document acDoc = Application.DocumentManager.MdiActiveDocument; acDoc.BeginDocumentClose -= new DocumentBeginCloseEventHandler(docBeginDocClose); } public void docBeginDocClose(object senderObj, DocumentBeginCloseEventArgs docBegClsEvtArgs) { // Display a message box prompting to continue closing the document if (System.Windows.Forms.MessageBox.Show( "The document is about to be closed." + "\nDo you want to continue?", "Close Document", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) { docBegClsEvtArgs.Veto(); } }
Private Sub AcadDocument_BeginDocClose(Cancel As Boolean) ' This procedure intercepts the Document BeginDocClose event. If MsgBox("The document is about to be closed." & _ vbLf & "Do you want to continue?", vbYesNo, _ "Close Document") = vbNo Then ' Veto the document close Cancel = True End If End Sub