UserControl_Terminate event not firing...

yvee
yvee used Ask the Experts™
on
I am developing an application using VB6. On my main form, I have a 3rd party ActiveX control.

My problem is that when I run my program, the terminate event (UserControl_Terminate) doesn't seem to fire when I close my form. I read somewhere that when the user closes the form that is hosting the control, this event gets fired

The funny thing is that when I select "Run -> Start" in VB6 everything works fine, but when I build an executable file and run it, the subroutine doesn't get called. Should I be calling the event directly?

Any ideas on why this could be happening?

Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here's some info I found on this topic I don't know if it applies to you:
http://support.microsoft.com/default.aspx?scid=KB;en-us;q248985

Commented:
When you shut down your app, are you using the End keyword?  That will immediate shut down the app, and may be preventing the Terminate event from occurring?

Check your app's Unload sequence to ensure that it doesn't end with this:

private sub form_Unload()
...
  End
End Sub

Author

Commented:
Here is how I shut down my app;

Me.Hide
Unload frmPrintWatch 'frmPrintWatch is the name of my form

If this is not the right way, then what is?

Thanks
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Here is how I shut down my app;

Me.Hide
Unload frmPrintWatch 'frmPrintWatch is the name of my form

If this is not the right way, then what is?

Thanks

Commented:
Is the user-control located on frmPrintWatch?  If so, it would seem that it should fire.  Do you have access to the source code for that control?  If not, how do you know that the Terminate event is not firing?


(And I'm not sure why you're hiding a form when you are shutting it down.  I don't think it should have any impact, but it doesn't seem to have any benefit either.)

Author

Commented:
Yeah, the user control is located on frmPrintWatch. For testing purposes, I added a "Msgbox" statement in the subroutine "UserControl_Terminate". I know the Terminate event is not firing 'cos I don't see the message when I shut down the app.

Commented:
I'd suggest trying to add the usercontrol to a new project and see if the terminate event fires.  If so, then there's some other code in you main project that's preventing it from firing.

If it were a class, I'd suggest setting the object to nothing, but when it has an interface (like a usercontrol does) it should automatically go away when the form unloads....hmmm...

Did you try:

Set frmPrintWatch = nothing
?
Maybe the forms not really unloading because something else in it is still pending or is forcing it to reload.
Also check to see if you have an object reference to the usercontrol in another form that IS still loaded.

Author

Commented:
I'm pretty sure the control is only being refrenced by my application.

What about the fact that when I run the application in VB6 (i.e. when I hit F5) it works fine?
VB does some internal management while in the IDE.  I'm surprised we don't have more instances of things working in the IDE that don't work while compiled.  <I'm pretty sure the control is only being refrenced by my application.>  I assumed that, what I was asking is : Do you have a variable reference to the ActiveX Control somewhere else in your application?  You could set a variable to the control like: Set oControl = MyUserControl1.  If that reference isn't completely freed up, no terminate event.

You will only receive a Terminate event if ALL references to a specific instance of your UserControl are destroyed.  This includes the one on the form (which should get destroyed on form_terminate) and all variables that point to that control.  Somewhere in the code you may have set a global reference to the usercontrol, and it's not unloading.  Try setting the usercontrol = nothing before closing the form.

Commented:
Yes.  One way to find out is to rename the object, then do a search-and-replace for that name.  If the name appears anywhere outside the form, it *may* be a problem (although not necessarily.)

After doing that, start the app in the IE using Ctrl-F5.  If you get any warnings about it not finding a reference, that's probably the culprit.

Aside from that, how tough would it be for you to load that form into a new project?  With that, you could also probably tell if the terminate is working correctly.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- refund and close
Please leave any comments here within the
next seven days.
Force accepted

** Mindphaser - Community Support Moderator **

Commented:
I've had the same problem, and have found following solution:

SubClass WindowProcedure of UserControl, when you get Window-Message WM_SHOWWINDOW (lParam=0), call UserControl_Terminate Event by your own.

Works fine.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial