In VBA I have a routine that used the Application's AppActivate and AppDeactivate events. Also, in VBA I used the BeginCommand and EndCommand events. Now, in .NET I am not sure how to use them. I put together a class to use these events in .NET. It seems to be working, but I am not sure of what I am doing. Could someone who has been using these events in .NET take a look at the following class and let me know what needs to be improved or changed. Basically, I am looking to achieve something similar to what I was using in VBA. Any help will be greatly appreciated.
Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.EditorInput Imports Autodesk.AutoCAD.Runtime Public Class MyAppEventClass Implements Autodesk.AutoCAD.Runtime.IExtensionApplication Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize AddHandler Application.DocumentManager.DocumentCreated, AddressOf Me.CallBack_DocumentCreated Call Me.IntializeEvents() End Sub Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate 'I am not sure if I really need to remove the handlers End Sub Private Sub CallBack_CommandEnded(ByVal sender As Object, ByVal e As CommandEventArgs) Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor ed.WriteMessage(vbCrLf & "A command is about to end.") End Sub Private Sub CallBack_CommandWillStart(ByVal sender As Object, ByVal e As CommandEventArgs) Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor ed.WriteMessage(vbCrLf & "A command is about to start.") End Sub Private Sub CallBack_DocumentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs) Me.IntializeEvents() End Sub Private Sub CallBack_DocumentToBeActivated(ByVal seneder As Object, ByVal e As DocumentCollectionEventArgs) Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor ed.WriteMessage(vbCrLf & "A document is about to be activated.") End Sub Private Sub CallBack_DocumentToBeDeactivated(ByVal seneder As Object, ByVal e As DocumentCollectionEventArgs) Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor ed.WriteMessage(vbCrLf & "A document is about to be deactivated.") End Sub Private Sub CallBack_DocumentActivationChanged(ByVal seneder As Object, ByVal e As DocumentActivationChangedEventArgs) Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor ed.WriteMessage(vbCrLf & "Document activation has changed.") End Sub Private Sub IntializeEvents() Dim Doc As Document For Each Doc In Application.DocumentManager AddHandler Doc.CommandWillStart, AddressOf Me.CallBack_CommandWillStart AddHandler Doc.CommandEnded, AddressOf Me.CallBack_CommandEnded Next AddHandler Application.DocumentManager.DocumentToBeActivated, AddressOf Me.CallBack_DocumentToBeActivated AddHandler Application.DocumentManager.DocumentToBeDeactivated, AddressOf Me.CallBack_DocumentToBeDeactivated AddHandler Application.DocumentManager.DocumentActivationChanged, AddressOf Me.CallBack_DocumentActivationChanged End Sub Private Sub TerminateEvents() 'Handler removal here End Sub End Class
Hi John,
Your VB.Net events do not work because you need to keep a member variable inside your class for each object you are listening events from, otherwise .Net will not maintain the callback on an object that is garbage collected.
Here is a small example. Also remember that .Net classes are instanciated once per document, the first time you run a command in that document, so this behavior may influence your events as they depend on member variables.
if you want to have an application wide event that doesn't relate directly to a specific document, you can use the keyword "Shared", so the member is instanciated only once.
Public Class PerDocManager Implements IExtensionApplication Private Shared _DocumentManager As DocumentCollection '/////////////////////////////////////////////////////////////////////////////////////////////////// '// '// '/////////////////////////////////////////////////////////////////////////////////////////////////// Public Sub Initialize() Implements IExtensionApplication.Initialize 'Un-comment if you want to demandload the dll after the first loading 'Otherwise run command "RegisterMe" to manually register for demandloading 'Run command Unregister to delete demandloading registry entry 'RegisterMe() _DocumentManager = Application.DocumentManager AddHandler _DocumentManager.DocumentActivated, AddressOf DocumentActivated AddHandler _DocumentManager.DocumentToBeDestroyed, AddressOf DocumentToBeDestroyed End Sub '/////////////////////////////////////////////////////////////////////////////////////////////////// '// '// '/////////////////////////////////////////////////////////////////////////////////////////////////// Public Sub Terminate() Implements IExtensionApplication.Terminate RemoveHandler _DocumentManager.DocumentActivated, AddressOf DocumentActivated RemoveHandler _DocumentManager.DocumentToBeDestroyed, AddressOf DocumentToBeDestroyed End Sub
I hope it helps.
Philippe Leefsma
Developer Consultant
Developer Technical Services
Global Subscription & Support
Autodesk EMEA
Can't find what you're looking for? Ask the community or share your knowledge.