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:
Enable a Document object event
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();
}
}