how to correctly dispose/close a form?

G

Guest

Public Class MainForm
Dim frm As DetailFrm '--frm is a Form Level variable

Private Sub MainForm_Load(...)
frm = Nothing
....
End Sub

Private Sub btnOpenDetail_Click(...)Handles btn.Click
If frm Is Nothing Then
frm = New frmDetailData
frm.Show()
Else
frm.BringToFront()
End If
End Sub

DetailFrm is not showdialog. It is basically a datasheet view of data
underlying MainForm. If frm Is Nothing, I instantiate it

frm = New frmDetailData

If frm is already running but is behind MainForm, when I click on
btnOpenDetail again - it will bring frm to the front. I don't want to
instantiate another copy of frm. The problem is that if I close frm
(DetailFrm) - by clicking on the red X at the top Right corner of the form,
and then click btnOpenDetail again - frm is still something/running so
frm.BringToFront() gets called but frm is not visible - does not appear.

In the frm.Disposed event I added Me.Dispose. But after closing frm - it
is still something/running. For this scenario - where frm is not showDialog,
what is the correct way to close/dispose frm? Is there a way to set it to
nothing from within the frm or will MainFrom need some poling mechanism that
checks a Property value from frm to see if the value = "YesRunning" or
"NoRunning" and if the value is "NoRunning" then set frm to frm = Nothing?

My current workAround is to set a Property Value in frm. If
frm.IsRunning.Equals(True) then frm.BringToFront(). If
frm.IsRunning.Equals(False) then I re-instantiate frm, but I believe it is
just another copy so now I have multiple copies running - but only one is
visible at a time. How to fix this?

Thanks,
Rich
 
T

Tom Shelton

Public Class MainForm
Dim frm As DetailFrm '--frm is a Form Level variable

Private Sub MainForm_Load(...)
frm = Nothing
...
End Sub

Private Sub btnOpenDetail_Click(...)Handles btn.Click
If frm Is Nothing Then
frm = New frmDetailData
frm.Show()
Else
frm.BringToFront()
End If
End Sub

DetailFrm is not showdialog. It is basically a datasheet view of data
underlying MainForm. If frm Is Nothing, I instantiate it

frm = New frmDetailData

If frm is already running but is behind MainForm, when I click on
btnOpenDetail again - it will bring frm to the front. I don't want to
instantiate another copy of frm. The problem is that if I close frm
(DetailFrm) - by clicking on the red X at the top Right corner of the form,
and then click btnOpenDetail again - frm is still something/running so
frm.BringToFront() gets called but frm is not visible - does not appear.

In the frm.Disposed event I added Me.Dispose. But after closing frm - it
is still something/running. For this scenario - where frm is not showDialog,
what is the correct way to close/dispose frm? Is there a way to set it to
nothing from within the frm or will MainFrom need some poling mechanism that
checks a Property value from frm to see if the value = "YesRunning" or
"NoRunning" and if the value is "NoRunning" then set frm to frm = Nothing?

My current workAround is to set a Property Value in frm. If
frm.IsRunning.Equals(True) then frm.BringToFront(). If
frm.IsRunning.Equals(False) then I re-instantiate frm, but I believe it is
just another copy so now I have multiple copies running - but only one is
visible at a time. How to fix this?

Thanks,
Rich

Rich,

Just let the mainform handle the frm.Closed event.
Private Sub btnOpenDetail_Click(...)Handles btn.Click
If frm Is Nothing Then
frm = New frmDetailData
addhandler frm.FormClosed, addressof Me.frm_Closed
frm.Show()
Else
frm.BringToFront()
End If
End Sub

private sub frm_Closed (byval sender....)
removehandler frm.formclosed, addressof me.frm_closed
frm = nothing
end sub
 
G

Guest

Thanks. That worked perfectly. For posterity - here is what I did

Private Sub frm_Closed(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosedEventArgs)
frm = Nothing
End Sub

The trick was to get the correct set of event args - which I got from the
FormClosed Event

Thanks again.
 
C

Cor Ligthert [MVP]

Rich,

I am always curious why people want to spent time to close or finalize
something earlier than strictly needed. Maybe you have an explanation for
that. That one that is busy to finalize is surely not taken if you have
created a new one.

Cor
 

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