I have two Issue using a MDI app.
1-at this moment I'm able to open MDI child forms using the MDIparent
except when the I opens form1 I'm also able to open forms 2
and 3 .
but I only want one form to be open at a time.
2- The next issue is I have form 2 to be opened from form1 and I would like to close form2 and form1 when click close on form2. At this moment I have to close form2 then click close on form1.

Thank you
code for Q1 This is the code on the MDIparent to open the forms.

Public Sub tbbCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbbCustomer.Click
        Dim CustomerMDIChild As New CustumerRprt()
        CustomerMDIChild.MdiParent = Me
        CustomerMDIChild.Show()
        CustomerMDIChild.Location = New Point(Me.Width / 2 - CustomerMDIChild.Width / 2, Me.Height / 2 - CustomerMDIChild.Height / 2)
    End Sub

Q2 here is he code I tried to close form2 and form1.

Public Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

        If m_ID = 0 Then
            Me.Close()
        Else
            CustumerRprt.Close()
            Me.Close()
        End If
    End Sub

Recommended Answers

All 10 Replies

use a loop that will check the mdichild's forms then close them and then open the form you wanted to open...

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

Dim frm1 As Form1
frm1.Show

I have two Issue using a MDI app.
1-at this moment I'm able to open MDI child forms using the MDIparent
except when the I opens form1 I'm also able to open forms 2
and 3 .
but I only want one form to be open at a time.

try to use ShowDialog() instead of Show() or check for TopMost = True

Thanks Jireh and samir_ibrahim for your replies.
The first Issue has been solved.
I still need more help with the second Issue.
This is the coed I used to open form1.

For Each Form In Me.MdiChildren
            Form.Close()
        Next
        Dim CustomerMDIChild As New CustumerRprt()
        CustomerMDIChild.MdiParent = Me
        CustomerMDIChild.Show()

This coed opens form2.

Design_Report.Show()

How can I close form2 and form1 by just closing form2

For Q2:
In form2, use the FormClosing event to close form1, then form2 will close after.

Private Sub Form2_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Form1.close
    End Sub

-Zander

I have tried your code but I'm still unsuccessful. Still can't close form1. form2 closes but not form1.
When I click the Close button it jumps to the closing sub (your code)
but does not close form 1.
Help

Is form1 set to the form that ends the app?
if it is, try Application.Exit in the form2 closing event.

-Zander

I have tried your code but I'm still unsuccessful. Still can't close form1. form2 closes but not form1.
When I click the Close button it jumps to the closing sub (your code)
but does not close form 1.
Help

I had tried this and works with me.

'in form2.button_click
Me.Close()
Form1.Close()

Thank you for your replies but it still isn't solved.
To Zandermander your code worked but it closes the entire appl. and I only want form1 and 2 to close.
To Samir_Ibrahim yes I have tried your coed and it did not work.
It closes only form1 but it does not close form2

Ok.. well try form1.hide maybe.. it doesn't "close it" but it should make it non visable.. can't test it right not.. on my phone when I get to my computer ill try some more things... though im not sure why form1.close won't work...

maybe use the code

form2.button_click
Me.Close()
Form1.Close()

but switch form1.close and me.close

sorry if there are errors in this post. once again.. on my phone.

-Zander

thanks all for your help.
I have a solution for my Issue.

In form1 I have the following code.

Form2.myParentForm = Me
            Form2.Show()

and in form2 this

Public myParentForm As Form
Me.Close()
            If (Me.myParentForm IsNot Nothing) Then
                Me.myParentForm.Close()
            End If
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.