• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Determining when a form is really closed

I have an MDI for with child forms created as needed.
I have them set up to auto-arrange (me.LayoutMdi) so I can see all of them at the same time.  If I add a form, they autoarrange just fine.

The problem is that when I close a form, they don't autoarrange.
I intercepted the Closed event but apparently the form is not closed yet.

How do I know when a form is really closed?

(If this doesn't have a direct answer, I'll bump up the points and I'll take the one that has the most elegant and/or simple solution to rearranging the remaining forms after one closes.)

To confirm that a form is not closed when the closed event occurs, try the code below.  If a form is really closed then it should have no name (or a default name) when it is closed.

    Private WithEvents frm As Form1

    Private Sub frm_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles frm.Closed
    End Sub

    Private Sub lblCurrentLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCurrentLine.Click
        frm = New Form1
        frm.Name = "anything"
    End Sub
1 Solution
If Not IsNothing(frm) Then ''form is not closed

Else '' form is closed

End IF
rspahitzAuthor Commented:
Sorry...I need an event that tells me when it's closed...
(It's: how do I know WHEN a form is closed, not IF it is "closed")
in every form is a "fromclosing" and a "formClosed" event...you can use that to capture the event and put whatever code you need to...pass something back to the mdiparent or a msgbox or whatever
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

rspahitzAuthor Commented:
The Closing and Closed events occur BEFORE the form closes.  See my sample code in the question.
well... the closest you can get is Closed and since you find out that the form is not actually closed when it's Closed ;) you may want to use some kind of a trick...

like starting a timer in your Closed, which set it to 100 ms, in the timer check for the name of a form and if it's closed, launch whatever method you want to notify your that the form IS closed, and stop the timer.

In theory, it should be on the first call (the first 100 ms).

good luck,
Bob LearnedCommented:
I believe that the one way would to monitor when the Finalize method is called (when the form is disposed), but that requires that the form is disposed:

Protected Overrides Sub Finalize()
End Sub

rspahitzAuthor Commented:
The finalize seemed like it could lead to something...I intercepted several events, like Dispose, etc, but they hadn't gotten rid of the form yet.
And for some reason, the Finalize never gets fired!

I was hoping to avoid the timer, but the idea of turning it on until the form is blank seems good enough.  I really wish that the MDI form would notify you if one of its children closes.

Anyway, here's bits of the code I used (for future reference, with the names changed to protect the innocent):
Child form:

    Private Sub frmChild_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Dim frm As frmMDIParent = Nothing

        If TypeOf (Me.Parent) Is frmMDIParent Then
            frm = Parent
            If TypeOf (Me.Parent.Parent) Is frmMDIParent Then
                frm = Parent.Parent
            End If
        End If

        If Not IsNothing(frm) Then
            With frm
                frm.ChildFormBeingClosed = Me.Text
                frm.tmrWaitingForFormClose.Enabled = True
            End With
        End If
    End Sub

Parent form:

    Private mptrChildForm As IntPtr
    Public WriteOnly Property ChildFormBeingClosed() As IntPtr
        Set(ByVal Value As IntPtr)
            mptrChildForm = Value
        End Set
    End Property

    Private miTimerCntr As Integer = 0
    Private Sub tmrWaitingForFormClose_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrWaitingForFormClose.Tick
        If Not ChildFormFound(mptrChildForm) Then
            Me.tmrWaitingForFormClose.Enabled = False
        End If
    End Sub

    Private Function ChildFormFound(ByVal FormName As IntPtr) As Boolean
        Dim frm As Form
        Dim bChildfound As Boolean = False

        For Each frm In Me.MdiChildren
            If frm.Handle.ToString Is FormName.ToString Then
                bChildfound = True
                Exit For
            End If
        Next frm

        Return bChildfound
    End Function

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now