Discussion:
How can a modeless form totally destroy itself
(too old to reply)
Peter T
2007-03-08 12:38:33 UTC
Permalink
The single form is opened modeless from a dll's entry class. In the button
close I want it destroyed, which 'Unload Me' does not do, and nor does 'Set
myFormName (or an external ref to the form) = Nothing'

I guess the solution must have been demonstrated in this ng, apologies if it
has but I can't find it. I have though found numerous descriptions as to
what's going on, including "Life Cycle of Visual Basic Forms". Also,
recommendations from outside the form best to set any ref to it and/or
FormName = Nothing. But I can't see how that can work if called directly or
indirectly from inside the modeless form. Ideally I would like to see the
form's terminate event fire.

Regards,
Peter T
Larry Serflaten
2007-03-08 13:03:43 UTC
Permalink
Post by Peter T
The single form is opened modeless from a dll's entry class. In the button
close I want it destroyed, which 'Unload Me' does not do, and nor does 'Set
myFormName (or an external ref to the form) = Nothing'
One possible cause for a form not being removed from memory is some
outside reference that still points to the form. If there are no outside references
then setting the form name to Nothing will (after unloading) cause the form to
be removed from memory. A simple test will show that is the case:

' In Form1:
Private Sub Command1_Click()
Form2.Show
End Sub


' In Form2:
Private Sub Form_Terminate()
MsgBox "Form2 removed from memory"
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set Form2 = Nothing
End Sub

If you always use the form name to call the form, then that will be the case.

If you declare other variables of the Form2 type, then you have to be careful
to manage those so that they release their reference when not in use.
(And setting the Form name would have no effect....)

LFS
RB Smissaert
2007-03-08 14:09:46 UTC
Permalink
I had a similar problem due to outside references holding on to the form
and found that this was caused by the fact that the form initialize event
did happen when I didn't expect it to happen.
The solution was to get rid of any code run by the initialize event and make
a normal Sub with the same code, and only run that Sub when I want it to
run.

This was actually in VBA, but the principle is the same. I always do this
now
and I find it avoids a lot of trouble.

RBS
Post by Larry Serflaten
Post by Peter T
The single form is opened modeless from a dll's entry class. In the button
close I want it destroyed, which 'Unload Me' does not do, and nor does 'Set
myFormName (or an external ref to the form) = Nothing'
One possible cause for a form not being removed from memory is some
outside reference that still points to the form. If there are no outside references
then setting the form name to Nothing will (after unloading) cause the form to
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Form_Terminate()
MsgBox "Form2 removed from memory"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set Form2 = Nothing
End Sub
If you always use the form name to call the form, then that will be the case.
If you declare other variables of the Form2 type, then you have to be careful
to manage those so that they release their reference when not in use.
(And setting the Form name would have no effect....)
LFS
Peter T
2007-03-08 14:35:17 UTC
Permalink
Post by Larry Serflaten
Post by Peter T
The single form is opened modeless from a dll's entry class. In the button
close I want it destroyed, which 'Unload Me' does not do, and nor does 'Set
myFormName (or an external ref to the form) = Nothing'
One possible cause for a form not being removed from memory is some
outside reference that still points to the form. If there are no outside references
then setting the form name to Nothing will (after unloading) cause the form to
Private Sub Command1_Click()
Form2.Show
End Sub
Private Sub Form_Terminate()
MsgBox "Form2 removed from memory"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set Form2 = Nothing
End Sub
If you always use the form name to call the form, then that will be the case.
If you declare other variables of the Form2 type, then you have to be careful
to manage those so that they release their reference when not in use.
(And setting the Form name would have no effect....)
LFS
That's unequivocal, thank you Larry.

I was sure I had tried that along with a myriad of other ways. I can only
assume other surrounding complexities blinded me to the obvious.

Thanks again,
Peter T

Loading...