Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Late Binding Events 1

Status
Not open for further replies.

SpiderBear6

Programmer
Sep 17, 2003
422
AU
Hi guys,

Could someone please point me in the right direction to figuring out how to respond to events of a late bound object? I know how to access the properties and methods of the objects, but don't know how to hook into the events.

Cheers all.


--------------------------------------
"We are star-stuff. We are the universe made manifest trying to figure itself out."
-- Delenn in Babylon 5 - "A Distant Star"
 
Check out WithEvents...

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Hello SpiderBear6,

You cannot trap events with late binding.

Since I don't know how much you know about event hooking here is a short primer, please don't be offended if you already know all of this stuff. For purposes of our discussion, let's say you want to work with Excel.

1. Events can only be trapped in a class module so they are not possible in a standard module. If a class module sounds too hard for you, remember that form code is just a special case of a class module.

2. Insert a new class module.

3. Set reference to current Microsoft Excel Object Library.

4. Define a module level variable for the application:
Private WithEvents mobjExcel As Excel.Application

5. Now that you have done the above, if you click on the left combobox in the editor you will see mobjExcel in the list. If you select it then the right combobox will display the list of available events.

6. Select the event you want to deal with by clicking it and the VBE will create a procedure block for you.

7. Place your code in the procedure block as appropriate.

8. Once you are done testing, go through your code and change all of your object references to Object (eg Private WithEvents mobjExcel As Object) to convert them to late binding. Don't forget to change your custom code which may have passed an Excel object as an argument.

I hope this helps. My apologies if I missed something as I need to leave shortly and I wanted to at least get you started. Good LucK!

Have a great day!

j2consulting@yahoo.com
 
As far as I know (butI'm not realy sure): using late-binding the compiler doesn't know what events to handle, so it just can't be done.
If you care to define an interface, to be implemented by the different classes you want to uses in this context, you can avoid late binding and use event-handling.
(I seem to be the only person lurking on this forum that sees an advantage in using interfaces ;-))

Of course, if the 'object' you refer to is a class you don't code yourself, it's getting more difficult: you'll have to build a wrapper-class.

If finally, the class might not be registered at all (if your're addressing Word, but possibly there is no Word installed at all): that is a situation where just can't use event-handling, because the event itself can not be definded. (as far as I know, but you'll always see some clever guy like strogm...)
 
Thanks for all of your replies guys. But I have found the solution. You can late bind events by using the VbControlExtender object instead of the generic object. You can then code against the ObjectEvent sub determining the event from the passed in EventInfo parameter.

Cheers.

--------------------------------------
"We are star-stuff. We are the universe made manifest trying to figure itself out."
-- Delenn in Babylon 5 - "A Distant Star"
 
Hello SpiderBear6,

Thanks for posting what you found. What reference to you set to be able to use your code and could you post an example as I am not familiar with it. Thanks again!

Have a great day!

j2consulting@yahoo.com
 
I'll be fascinated to see your solution for late bound objects in general. As far as I was aware this only worked for controls and even then has a number of limitations (which is still better than nothing, I suppose)
 
I didn't even really know about the vbControlExtender object and it's full potential until I had to do some digging to do what I wanted to do.

Code:
Private WithEvents m_ABC As VBControlExtender

Private Sub m_ABC_ObjectEvent(Info As EventInfo)

    With Info
        Select Case .Name
            Case "Event1"
		' do stuff for the event "Event1"
			            
            Case "Event2"
		' do stuff for the event "Event1"
                
            Case Else ' Unknown Event
                ' Handle unknown events here.
        End Select
    End With
End Sub

Public Function StartABC(byref frm as Object) As Boolean
    Set m_frm = frm
    
    On Error Resume Next
    Call m_frm.Controls.Remove("ABCControl")
    On Error GoTo 0
    
    Set m_ABC = m_frm.Controls.Add("ABCAX.ABCViewerAXCtl.1", "ABCControl")
    
    StartABC = m_ABC.object.StartViewer

End Function

Sorry if the code isn't quite correct, I had to strip it out of my code and make it simple.

To respond to events of the late bound object, you use the "ObjectEvent" sub.

To use the methods of the late bound object, you need to go through the "object" object. For example, m_ABC.object.StartViewer. StartViewer is one of the methods of the ABC object.

I didn't have to add any extra or special references to use the vbControlExtender object.

It works really well. I would have preferred early binding but that wasn't possible in my particular situation.

Hope that helps someone.

Cheers.



--------------------------------------
"We are star-stuff. We are the universe made manifest trying to figure itself out."
-- Delenn in Babylon 5 - "A Distant Star"
 
Right - so it works for controls only, not objects in general.
 
Considering you use the vbControlExtender, I guess that would be correct. In my situation, the object (control) I was trying to late bind to had a control interface like the Timer control. I guess if the object you were trying to use didn't have such an interface, you could possible wrap it up in one.

Anyway, it's a great solution for my problem. So thanks for all of the replies and interest, guys.

Cheers all.

--------------------------------------
"We are star-stuff. We are the universe made manifest trying to figure itself out."
-- Delenn in Babylon 5 - "A Distant Star"
 
Great. As I said, I'm glad this helps solve your particular problem.

However, I think it important to point out that it is

a) specifically for controls (mostly usercontrols); and
b) not late binding

so that people looking for a generic, late binding objects solution don't mistakenly try and use this one

Although, as you suggest, it is probably possible to wrap non-usercontrol objects as usercontrols, and subsequently use the vbControlExtender interface that a usercontrol offers. An alternative would be to write one's own 'vbClassExtender' interface code to be added to any class. Probably a lot of work in both cases, and would be aided immensely if VB6 did inheritance...
 
How is it not late binding?

--------------------------------------
"We are star-stuff. We are the universe made manifest trying to figure itself out."
-- Delenn in Babylon 5 - "A Distant Star"
 
Whether you get early or late binding is dependant on how you declare your object, not on how you get an instance of it. Your 'New', CreateObject, etc. are irrelevant to the type of binding you get.

You basically only get late binding when you declare

Dim x as Object

Everything else gets you early binding.

In the case of the vbControlExtender this is just one of the Interfaces (I won't get into the differences between Interfaces and Classes/Objects here, except to say that VB does its very best to blur an important distinction) to a UserControl, and

Dim WithEvents x as vbControlExtender

is just another example of early binding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top