Anel Hodzic
Anel Hodzic

Reputation: 209

MDI Child form close on new open

How to close MDI Chid form when i want to open a new one.

On this way i open both of them but i want to close the previous when opening the new one.

 Private Sub DostupniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DostupniToolStripMenuItem.Click
        Dim frm As New FrmDostupniZaposlenici
        frm.MdiParent = Me
        frm.Show()
        frm.WindowState = FormWindowState.Maximized
    End Sub

Private Sub DodajToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DodajToolStripMenuItem.Click
    Dim frm As New frmDodajZaposlenika
    frm.MdiParent = Me
    frm.Show()
    frm.WindowState = FormWindowState.Maximized
End Sub

I have around 10 mdi child forms.

Edit :

New code. How to prevent to open form on form. Example i want to close all other mdi forms when new form is open. On this way if i click on 4 buttons in toolstrip i got 4 forms opened. I don't want that. If i click on button 3 i want to close the previous form and load the form3.

    Private Sub DostupniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DostupniToolStripMenuItem.Click
    FrmDostupniZaposlenici.MdiParent = Me
    FrmDostupniZaposlenici.Show()
    FrmDostupniZaposlenici.WindowState = FormWindowState.Maximized
End Sub

Private Sub DodajToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DodajToolStripMenuItem.Click
    frmDodajZaposlenika.MdiParent = Me
    frmDodajZaposlenika.Show()
    frmDodajZaposlenika.WindowState = FormWindowState.Maximized
End Sub

Private Sub IzmjeniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IzmjeniToolStripMenuItem.Click
    frmIzmjenaZaposlenika.MdiParent = Me
    frmIzmjenaZaposlenika.Show()
    frmIzmjenaZaposlenika.WindowState = FormWindowState.Maximized
End Sub

Private Sub ObrisiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ObrisiToolStripMenuItem.Click
    frmObrisiZaposlenika.MdiParent = Me
    frmObrisiZaposlenika.Show()
    frmObrisiZaposlenika.WindowState = FormWindowState.Maximized
End Sub

Upvotes: 2

Views: 5199

Answers (2)

Abraham Lawson
Abraham Lawson

Reputation: 11

To close an opened child form first, enter the following codes in the click event of the menu item, just immediately after declaring the child form.

ActiveMdiChild.Close()

Upvotes: 1

The Blue Dog
The Blue Dog

Reputation: 2475

Simply loop through all the open MDI child forms and close them ...

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

Upvotes: 1

Related Questions