mack
mack

Reputation: 2965

Close a form when another form closes

This should be simple, but I can't get it to work. I've searched SO and Google and amazingly I haven't found the answer. All I want to do is close one form when another form closes. The second form is opened on a button click. When Form1 closes, Form2 should also close. Form2 may not be open so we need to check to see if it's open first.

This is what I've been working with:

Private Sub frm_scu_config_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

    ' this isn't working
    If Application.OpenForms().OfType(Of frm_scu_report_display).Any Then

        Dim frmConfig As frm_scu_report_display

        ' Open the config form and pass the list of turbines
        frmConfig = New frm_scu_report_display()
        frmConfig.Close()

    End If
End Sub

Upvotes: 0

Views: 226

Answers (1)

KekuSemau
KekuSemau

Reputation: 6856

You already have the better half of the answer in that LINQ query, but then you create a new instance of frm_scu_report_display and try to close this (unopened) instance.
If I didn't get it wrong, it should work if you stay on the path you already had right:

    While Application.OpenForms().OfType(Of frm_scu_report_display).Any
        Application.OpenForms().OfType(Of frm_scu_report_display).First.Close()
    End While

Upvotes: 1

Related Questions