Determining when a form is really closed

Posted on 2006-03-20
Last Modified: 2010-04-23
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
Question by:rspahitz
    LVL 35

    Expert Comment

    If Not IsNothing(frm) Then ''form is not closed

    Else '' form is closed

    End IF
    LVL 22

    Author Comment

    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")
    LVL 5

    Expert Comment

    in every form is a "fromclosing" and a "formClosed" 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
    LVL 22

    Author Comment

    The Closing and Closed events occur BEFORE the form closes.  See my sample code in the question.
    LVL 21

    Accepted Solution

    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,
    LVL 96

    Expert Comment

    by:Bob Learned
    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

    LVL 22

    Author Comment

    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

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now