mdi form close problem; What am I missing??

J

Jeremy

I've spent many hours on this issue, so someone please either tell me I'm an
idiot, or give me a hint on where to look.

My mdi child has a shared execute method which creates and shows it. When I
close the form, it visibly goes away, but it remains in memory. I know this
because ComboBox events fire for every supposedly "closed" instance of
frmMyMDIchild. Obviously there is either a nasty bug in VB, or I completely
misunderstand something very basic. Help me out here!

- Jeremy

Public Class frmMyMDIchild
Inherits System.Windows.Forms.Form
Shared frm As frmMyMDIchild = Nothing
Private ImDead As Boolean = False 'for debugging

Public Shared sub Execute(mdiParent as Form)
If frm Is Nothing Then
frm = New frmMyMDIchild
With frm
.midParent = mdiParent
'Do some inits ...
End With
End If
frm.Show()
End Function

Private Sub frmMyMDIchild_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
Me.ImDead = True
frm.Dispose()
frm = Nothing
End Sub

Private Sub cbMyDropDown_SelectedValueChanged(ByVal sender as Object, ByVal
e As System.EventArgs) Handles cbMyDropDown.SelectedValueChanged
' **** This fires even for instances of frm where ImDead is True. ie: the
"Closed" frms are not closed at all.
msgbox(iif(ImDead,"Dead","Not Dead"))
End Sub

'** end **
 
C

Cor Ligthert

Jeremy,

I did not look real deep at your code. "Shared" means that it has to be in
memory from the begin untill the end of the program.

Can that be the problem?

Cor
 
J

Jeremy

Cor, yes, the variable does exist even if the class hasn't been
instantiated. Maybe I misunderstand how "shared" works. Are you saying
that if you create a form and assign it to a shared var, it can never be
disposed?

The problem I'm seeing is that even if the form is closed, it still exists
and is still receiving combobox events. Yet the edit boxes have been
cleared -- I can inspect the objects on the dead form in the debugger, and
see their values.

Thanks for taking the time to think about this!

Jeremy
 
C

Cor Ligthert

Jeremy,
Cor, yes, the variable does exist even if the class hasn't been
instantiated. Maybe I misunderstand how "shared" works. Are you saying
that if you create a form and assign it to a shared var, it can never be
disposed?

You are now mixing up Diposed as well.

Disposed is only give it to the GC to let it check that if it can be deleted
and do that when that is possible.

For the rest the meaning of your text right, although not reading your code
again, was my thought that you had written that the form was shared (which
means forever and from any place usable in your program).

Cor
 
J

Jeremy

Cor, really, the code isn't _that_ hard to read, is it (g)? Anyway, maybe
I'm stuck on symantics, but that could be the root of my troubles. Here's a
couple of lines from the top of my form's code.

Public Class frmMyMDIchild
Inherits System.Windows.Forms.Form
Shared frm As frmMyMDIchild = Nothing
.... etc.

The form class is public. The variable frm is indeed shared. Please
correct me if I'm wrong, but I believe that frm holds a pointer to the
form's resources. If I do

frm = new frmMyMDIchild
frm.show()
....
frm.close(),

the form should cease to be an active part of my application, although GC
hasn't gotten around to cleaning it up. Am I completely wrong on this?

Jeremy

Cor Ligthert said:
....> For the rest the meaning of your text right, although not reading your
code
 
C

Cor Ligthert

Jeremy,
Cor, really, the code isn't _that_ hard to read, is it (g)?

No however I thought I remembered me this.
Public Class frmMyMDIchild
Inherits System.Windows.Forms.Form
Shared frm As frmMyMDIchild = Nothing
... etc.

And shared means just what I wrote already, it is a solid part of the
program .

However what is it you are after, because this is not normal use of the MDI?

Cor
 
J

Jeremy

Thanks for sticking with me on this, Cor, I'm grateful.

What I want is to have an Execute sub in my form that handles create,
initialize, show. That way I can have a single line of code in the calling
program. I am also trying to implement the singleton pattern so there is
only one instance at a time of this particular form.

The Sub Execute(parms) technique works perfectly for non-mdi forms, and in
mdi apps in other windows programming languages. In fact, it seems to work
for vb mdi apps without comboboxes.

Rgds, Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top