Link to home
Create AccountLog in
Avatar of Cannibal_Jack
Cannibal_Jack

asked on

Closing multiple forms

Hi, would someone be able to supply me with the code for this... If one form is closed to also close another form. Any help would be great, thanks
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

You can do thin on close event:

My.Application.OpenForms.Item("YourFormName").Close()
Avatar of Cannibal_Jack
Cannibal_Jack

ASKER

This might sound stupis but how do I enter that into the form close event?
You can do this on the form that you want to close the other:

Private Sub frm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        My.Application.OpenForms.Item("YourFormNameToClose").Close()
    End Sub
Hi I'm getting this error...
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

on this line...
My.Application.OpenForms.Item("Form2").Close()
Then from of the name is "Form2" ?
The form I put the code in is form1 and when form 1 is closed I want form2 to close also.
I dont know if this helps but form2 is actually the mdi parent and form one is the child.
Try this:

    Private Sub Form2_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        For Each frm As Form In Me.MdiParent.MdiChildren
            If frm.Name = "Form3" Then
                frm.Close()
            End If
        Next
    End Sub

Attention: This event will fire also when you close the MDI form.
When you create your MdiChild (Form1), use AddHandler() to wire up its FormClosed() event.  Completely Dispose() of it though before you close the MdiParent:
Public Class Form2
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm1 As New Form1
        frm1.MdiParent = Me
        AddHandler frm1.FormClosed, AddressOf frm_FormClosed
        frm1.Show()
    End Sub
 
    Private Sub frm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
        Dim frm As Form = DirectCast(sender, Form)
        frm.Dispose()
        frm = Nothing
 
        Me.Close()
    End Sub
 
End Class

Open in new window

I have tried both solutions and neither seem to work,I think maybe I'm getting a little muddled up here.
Ive started a new project now I have a form called MDI_Container_Form and a form Called Test_Form. Test_Form Is a child to MDI_Container_Form and when the program is run Test_Form opens up instantly inside MDI_Container_Form. What I' like to do is when the red cross next to minimize and maximize is press on Test_Form that it also closes the MDI_Container_Form.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
What is the purpose of the MDI container if you want the whole app to close when the child is closed?

Wouldn't a single form suit this task better?  I don't think you are giving the big picture...     ?

Application.Exit() will certainly close both forms...but what if there are other forms you want to stay open?
>> Application.Exit() will certainly close both forms...but what if there are other forms you want to stay open?

Idle_Mind,

From is last post I understand that he have only two forms and want to close the application. Maybe I'm worng!!!
You're exactly right jpaulino...that is what he has DESCRIBED.

I'm just wondering what the point is!  Why bother with an MDI approach at all?  Killing the whole app seems like using a nuclear bomb to kill a fly.  Just wondering if we aren't getting the "bigger picture" here....
Thanks, I've sorted it with the code from above. The reason I wanted to do it this way was because the child form was a login window and if the login window was closed there would be no point in the container window being open. thanks for all your help guys.