Hi cadmaxza.
What your asking is something more complex, but I already done it. This is your new class:
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Public Class ManageDoc
Private Shared WithEvents m_docMan As DocumentCollection
Private Shared m_doc As Document
Private Shared m_docsTable As Hashtable
Public Sub New()
m_Doc = Application.DocumentManager.MdiActiveDocument
DocumentEvents()
End Sub
Private Shared Sub m_doc_CommandEnded(ByVal sender As System.Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.CommandEventArgs)
If e.GlobalCommandName = "LAYER" Then
MsgBox("Kilroy was here")
Else
MsgBox(sender.ToString)
End If
End Sub
Private Shared Sub DocumentEvents()
m_docsTable = New Hashtable
collectAllDocs()
m_docMan = Application.DocumentManager
End Sub
Private Shared Sub collectAllDocs()
Try
Dim docEnum As IEnumerator = Application.DocumentManager.GetEnumerator()
While docEnum.MoveNext()
Dim doc As Document = CType(docEnum.Current, Document)
addDoc(doc)
End While
Catch ex As System.Exception
MessageBox.Show(ex.Message + vbCrLf + vbCrLf + ex.StackTrace)
End Try
End Sub
Private Shared Sub addDoc(ByRef doc As Document)
If Not m_docsTable.ContainsKey(doc) Then
AddHandler doc.CommandEnded, AddressOf m_doc_CommandEnded
m_docsTable.Add(doc, True)
End If
End Sub
Private Shared Sub removeDoc(ByRef doc As Document)
If m_docsTable.ContainsKey(doc) Then
UndoADoc(doc)
m_docsTable.Remove(doc)
End If
End Sub
Private Shared Sub UndoADoc(ByRef doc As Document)
Try
If Not m_docsTable.Contains(doc) Then
Return
End If
m_doc = doc
RemoveHandler m_doc.CommandEnded, AddressOf m_doc_CommandEnded
Catch ex As System.Exception 'Caso ocorra alguma erro mostra uma mensagem de erro
MessageBox.Show(ex.Message + vbCrLf + vbCrLf + ex.StackTrace)
End Try
End Sub
Private Shared Sub m_docMan_DocumentToBeDestroyed(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs) Handles m_docMan.DocumentToBeDestroyed
removeDoc(e.Document)
End Sub
Private Shared Sub m_docMan_DocumentCreated(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs) Handles m_docMan.DocumentCreated
m_docMan = Application.DocumentManager
addDoc(e.Document)
End Sub
End Class
I've done this class taking ideias from the sample given by the sdk (EventsWatcher). If you need some kind of explanation of how it works just ask. I will not explain the class right now because I think It's easy to understand how it works, and it's ready to use. Anyway, if you need any explanation, feel free to ask me aind I will explain to you.
I hope it helps...
Regards,
Filipe Marcelino