How will i close all the forms within my main form? Everytime I click on the menus of my main form,I'm setting my main form as MDI-Parent form and then loading a new form as it it is the child form if I'm not mistaken. I have a log-out menu, and what I want to happen is everytime I log-out all the forms within my parent form will be closed. How will I do it? Here are some of my codes.

Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        Dim approval As New frmApproval
        frmApproval.MdiParent = Me
        frmApproval.Show()
    End Sub

------------------------------------

 Private Sub LogoutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogoutToolStripMenuItem.Click
        If MsgBox("Are you sure you want to log-out?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Exit") = MsgBoxResult.Yes Then
            Me.Hide()
            frmLogin.Show()
        Else
        End If
 End Sub

Hi,

You can do it like this:

For Each frmApproval As Form In Me.MdiChildren
            frmApproval.Close()
 Next

thanx it works, just a follow up question what if i have more forms opened how will i close all of them? should i do it like this?Is there a way that like all the forms are in an array so that I'll just work it with loop to close all of them?

For Each frmApproval As Form In Me.MdiChildren
                frmApproval.Close()
            Next

For Each frmOverride As Form In Me.MdiChildren
                frmOverride.Close()
            Next

For Each frmUser As Form In Me.MdiChildren
                frmUser.Close()
            Next

No, you don't have to close the other forms manually.

This will close all the MdiChildren forms:

For Each frm As Form In Me.MdiChildren
   frm.Close()
Next

You might have been mistaken by using "frmApproval" as the variable name. To better understand how the code works I've changed "frmApproval" for "frm" in the above code.

Thanks

Thanks I have been looking for this very code. Im new to visual basic and am learning a lot from DANIWEB.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.