Click to See Complete Forum and Search --> : Unloading a form from an event of a control


Nick A.
January 11th, 2000, 03:06 AM
I have made a custom button control in VB6(SP3) which contains a Click event. If i place this control in a form and in its Click event i put the following code, the program causes a GPF. Anyone know why is that? Note that this didn't happen in VB5!


private Sub CustomCommand1_Click()
Unload me
End Sub

Lonely Wolf
January 11th, 2000, 05:11 AM
Have you tried "unload formname"?

Nick A.
January 11th, 2000, 05:37 AM
Yes. Makes no difference. I assume that the "unload" statement causes the form to begin unloading and that causes the control to be unloaded while the Click event is pending. That must be the cause of the GPF. If that's the case though, i don't know how to work around it. Any help would be greatly appreciated.

Sigal Laniado
January 11th, 2000, 05:56 AM
Maybe you can try this:
when the user clicks the button set a flag bUnload=true.
In the form set a timer to check this flag and if it is true perform the unloading

Nick A.
January 11th, 2000, 06:13 AM
Thanx for your suggestion. I've tried this. In fact, this is the way i've done it until now and believe me, it's neither convenient nor effective. What i can't understand is how the classic button works. Also, why the same control worked fine under VB5? This thing is either a bug of VB6 or there is a way it can be done without causing a GPF and i just don't know it. I just hope someone knows a way around it.

P.S. I've tried putting some DoEvents in the code but no luck :(

Crazy D @ Work
January 11th, 2000, 06:29 AM
Actually I wonder why it doesn't work, I have a user control with a handfull of buttons, one of them is used to unload the form. It raises an event, and in that event (in the form), I use unload me and it works very well :-) Maybe it has to do with something else that's going on in your program.

Crazy D @ Work :-)

Nick A.
January 11th, 2000, 07:15 AM
Well, in my case, the control IS the button. I've put a Sheridan SSPanel in it. When a user clicks on the control, i emulate the PushDown effect by changing the BevelOuter value in the code. I can't find any flaws in the code. It works fine. The only problem it has is when i put an Unload <form> in the Click event. Most of the time it crashes (occasionally it works). And it had never crashed in VB5!?! Go figure. Anyway, i've learned to live with it. I just thought to post this problem here just in case there is something i missed. There isn't something special to do when firing an event, right? You just do a RaisedEvent xxx and that's all?

Chris Eastwood
January 11th, 2000, 07:38 AM
I've seen this problem before on a couple of controls (ActiveBar by DataDynamics and some of the Toolbars from vbAccelerator).

It's usually down to the control holding onto some reference (ie. the parent form) and then the parent closes down while the control still needs to perform some steps once the 'event' has returned.

As for why it works on VB5 and not on VB6 - I've no idea (in fact, I had more of this type of problem under VB5!). In most cases where this has been hard to find, I've just implement a slimy hack (as mentioned elsewhere in this thread), with a module level boolean that gets set to true, then a timer checking for this and unloading the form.

I've even managed to reproduce this effect/crash from ActiveX DLL's that raise an event to the client that then tries to clear down the references to it. vbwish@microsoft.com anyone ?


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Nick A.
January 11th, 2000, 07:48 AM
Well, i suppose it's up to Uncle Bill then.... :)

Anyways, thanks to you all for your answers...

Crazy D @ Work
January 11th, 2000, 08:09 AM
"vbwish@microsoft.com anyone ?"
they are never going to listen to all my wishes... *LOL*


Crazy D @ Work :-)

Bruno
January 11th, 2000, 11:33 AM
For its own controls, VB prevents crash by raising error 365: 'Unable to unload within this context'

' add combo box, paste this code
option Explicit

private Sub Combo1_Click()
Unload me ' error 365
End Sub

private Sub Form_Load()
Combo1.AddItem "item 1"
Combo1.AddItem "item 2"
End Sub