Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to close an MDI Child form programmatically

Status
Not open for further replies.

Goodall

MIS
Aug 4, 2004
18
US
Q. How can I close an MDI child form programatically, as opposed to the user closing the form with the top-right X?

Here is the scenerio.
- I have a listview control that lists inventory items.
- Each item contains the path to a .jpg file.
- When the user selects a listview item, I create a new MDI Child form that contains a scrollable picturebox.

This is working as fine as frog's hair. However, each time I select a new item, a new child form is created. I want only one child form created at any given time. Thus, when the user selects and item from the listview, I want to programmatically close the any previously created child form.

This is the code that is called from the Parent form when an item is selected from the listview.

Code:
    Sub CreateImageForm()
        Try
            Dim ImageForm As New frmImage
            ImageForm.MdiParent = Me
            ImageForm.Show()
            ImageForm.Location = New Point(426, 2)
            ImageForm.Text = ImgFile
            ImageForm.Name = "TheImageForm"
        Catch Excep As System.Exception
            MessageBox.Show(Excep.Message, "CreateImageForm Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

Thanks in advance for your comments.
 
MDI Parent forms contain all child forms in a .Forms collection I believe (it's been a while). You should be able to iterate through those forms and close any that are of the same type as the one that you want to open.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick,

I found the parents MDI child Forms collection. It is called .MdiChildren. How weird is that? :) I added the code below to close all MDI children in a parents Forms collection.

Thanks for the tip.

Code:
Sub DeleteImageForm()
    Try
        For Each f As Form In Me.MdiChildren
            f.Close()
        Next
    Catch Excep As System.Exception
        MessageBox.Show(Excep.Message, "DeleteImageForm
        Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top