Registering a Revit Event handler using VB

Registering a Revit Event handler using VB

Gary_J_Orr
Collaborator Collaborator
1,263 Views
7 Replies
Message 1 of 8

Registering a Revit Event handler using VB

Gary_J_Orr
Collaborator
Collaborator

I've searched, tried language conversion utilities, etc., etc... I simply can't find a working example (or anything in any version of help for the API on any site or in the Labs) of registering for Revit events.

 

The C# method would be:

thisApp.ControlledApplication.DocumentCreated += New EventHandler(Of DocumentCreatedEventArgs)(eventHandlerMethod)

or:

thisApp.ViewActivated += New EventHandler(Of ViewActivatedEventArgs)(eventHandlerMethod)

 

I believe that the += and -= for registering and unregistering events needs to be replaced with AddHandler and RemoveHandler in VB but I have been unable to find the correct synax and requirements for what needs to be/ can be passed to the handler function. From the C# examples and documentation I know that the eventHandlerMethod should be able to receive the sending object and the event arguments themselves.

 

Intellisense and Microsoft help tells me that the AddHandler function should look something like this:

 

AddHandler thisApp.ControlledApplication.DocumentCreating, AddressOf (eventHandlerMethod(origObject, DocumentCreatedEventArgs))

or:

AddHandler thisApp.ViewActivated, AddressOf (eventHandlerMethod(origObject, ViewActivatedEventArgs))

 

So how do I pass the event arguments (and, in the case of an modified object, the object that was modified... I know, I didn't provide such an example but, just thinking ahead here) to my "eventHandlerMethod" (as used in my example)?

 

-Gary

 

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager

aka (current and past user names)
GaryOrrMBI (MBI Companies 2014-Present), Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
0 Likes
Accepted solutions (1)
1,264 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

Hi Gary,

 

It seems like you've discovered that these event handlers are delegate instances. If that isn't necessarily the case: https://msdn.microsoft.com/en-us/library/ms172879.aspx

In adding the event handler you only need to pass the function which you intend to handle the event, no need to specify the arguments because the arguments are already specified by the delegate type declaration
more on delegates here: https://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx
Also, C#4.0 The Complete Reference has a great section on delegates that might help clarify.

When Revit calls your event handler it will pass in the pre-agreed upon arguments (as defined in the delegate type aka event) EventHandlers in .NET (I believe), are myEventHandler(object sender, EventArgs args) which allows you to define your own event handlers with this pattern of arguments which makes your "handler" function a "match" for the delegate type declaration for which you intend it to be a handler..

Here's another good read:
http://stackoverflow.com/questions/803242/understanding-events-and-event-handlers-in-c-sharp

All that should, in theory, be transmutatable to VB. (I am most definitely NOT and expert in VB so take that with the requisite grain of salt).

Hope I've understood your question and haven't given you a bunch of information you already know. But in the case that I have please feel free to clarify and we'll try to help however we can.

Message 3 of 8

Anonymous
Not applicable
oh, I just noticed that what you suggested for C# event handler registration isn't quite what we might expect to see. In C# to add an event handler it looks a lot like this:
revitApp.ControlledApplication.DocumentOpened += myDocOpenedHandler;

Note, there is no mention of arguments as they've already been agreed upon by the delegate type e.g. (object sender, DocuemntOpenedEventArgs [or something like that]) and the myDocOpenedHandler function definition just needs to adopt that set of parameters to be a "match"
0 Likes
Message 4 of 8

Gary_J_Orr
Collaborator
Collaborator

I'm lost when it comes to C#... What I posted was directly out of an example from Dev Labs (or maybe it was from the building Coder... or maybe from the samples in the SDK... or maybe from the API help... that's the problem with trying to figure anything out with the Revit API: no single clear and concise resource). So I assume that what I posted was the correct method and I have been trying to emulate it in VB.

 

-G

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager

aka (current and past user names)
GaryOrrMBI (MBI Companies 2014-Present), Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
0 Likes
Message 5 of 8

Anonymous
Not applicable

Hi Gary,

 

I understand that it can be frustrating: Please realize I'm only trying to help. 

 

The "Of" keyword in the C# example is not a keyword of C# language (https://msdn.microsoft.com/en-us/library/x53a06bb.aspx) it is rather a keyword of VB(https://msdn.microsoft.com/en-us/library/ksh7h19t(v=vs.90).aspx) so the statement:

//The C# method would be:
thisApp.ControlledApplication.DocumentCreated += New EventHandler(Of DocumentCreatedEventArgs)(eventHandlerMethod)

is not a C# event handler registration.  it would be like this:

thisApp.ControlledApplication.DocumentCreated += eventHandlerMethod;

the equivalent in VB:

AddHandler thisApp.ControlledApplication.DocumentCreated, AddressOf eventHandlerMethod

'with a function declared:
Private Sub eventHandlerMethod(ByVal sender As System.Object, ByVal args As DocumentCreatedEventArgs) Handles thisApp.ControlledApplication.DocumentCreated
   ' Add event handler code here.
End Sub

if you're trying to figure out how to pass additional arguments/information to the handler as parameters that can be accessed in the handler; you might consider storing that information somewhere (static variables, extensible storage, RevitParameters) or pass an anonymous method as the event handler as shown here http://stackoverflow.com/questions/4215845/c-sharp-passing-extra-parameters-to-an-event-handler but note that it is (I believe) evaluated at the time of registration. 

 

Best of luck with it.

Message 6 of 8

Gary_J_Orr
Collaborator
Collaborator

Thank ya much Ken... and no, I wasn't bashing you... that was Adesk bashing, sorry if it came across otherwise. I'll give your proposed solution a try as soon as I can get back on the project (got "real" work and all that 😉 but I'm sure that you have me on the correct path as this looks more like what I was expecting (both your C# correction for the simplified "direct" handler and the VB version thereof)

 

In looking at what you mentioned about "anonymous" methods... I think that is what I was trying to decipher from the SDK samples (Revit 2016 SDK\Samples\Events\EventsMonitor) without understanding what they were doing... and that's what was throwing me. They were using one handler for all events to simply log the events within the handler method. This must be the reason for all of the additional declarations within the assignment call.

 

There is also the addition of the "Handles..." within the function itself. The SDK example didn't address this because of how they were creating their handler.

Thanks again.

 

-G

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager

aka (current and past user names)
GaryOrrMBI (MBI Companies 2014-Present), Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
0 Likes
Message 7 of 8

PhillipM
Advocate
Advocate
Accepted solution

In VB it is something like this.

 

 AddHandler uiApp.ViewActivated, AddressOf ViewActivated

And then

 

Public Function ViewActivated(ByVal sender As Object, ByVal args As Autodesk.Revit.UI.Events.ViewActivatedEventArgs)
        
'your code here.

    End Function

I hope that helps?

Message 8 of 8

Gary_J_Orr
Collaborator
Collaborator
Phillip, yes, I'm sure it will end up being what I need... It's the same as Ken's suggestion (although his includes the "Handles..." in the function definition).
I just need time to get back into it and apply it.

Thanks,
-G
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager

aka (current and past user names)
GaryOrrMBI (MBI Companies 2014-Present), Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
0 Likes