How to establish a list of child forms currently shown on the MDI parent form.

Hello Experts.

I've come up with a nice method for embedding multiple child forms via MDI into a parent form using the FlowLayoutPanel object as a container for the various forms.

So on the main menu, the user might click "Show Form 1" and it appears in the MDI parent form. Next the user clicks "Show Form 2" and it would appear below Form 1 and automatically enable the vertical sroll bar so they can navigate between the forms. Finally, the user clicks "Show Form 3" and it appears below Form 2 compliments of the FlowLayoutPanel. The user now has three forms that they can access just by scrolling to the desired form.

How should I handle the process of removing forms from the FlowLayoutPanel? This would typically occur when the user clicks on the Exit control on any of the three forms.
I would also like to have general menu options to close specific forms and another to close ALL forms. Here's what I've come up with so far:
Public Sub CloseForms(ByVal WhichForm As String)
   Dim FormList As New List(Of Control)
   Dim FormCnt As Integer = FormList.Count - 1
   For Each frm As Form In getMyControls(FormList, frmMain.flpMain, GetType(Form))
       Select Case WhichForm
           Case "First"
               If FormList.Item(0) Is frm Then frm.Close()
           Case "Last"
               If FormList.Item(FormCnt) Is frm Then frm.Close()
           Case "All"
       End Select
End Sub

Open in new window

Just to translate the above code, Function getMyControls does this:
    Public Function getMyControls(ByVal ControlList As List(Of Control), ByVal parent As Control, ByVal ControlType As System.Type) As List(Of Control)
        If parent Is Nothing Then Return ControlList
        For Each child As Control In parent.Controls
            End Try
        Next child
        Return ControlList
    End Function

Open in new window

Also, frmMain is the MDI parent form, and flpMain is the FlowLayoutPanel docked inside of frmMain.

The error message that I am receiving is
System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.

By the way, I just now tried replacing the line
For Each frm As Form In getMyControls(FormList, frmMain.flpMain, GetType(Form))

Open in new window

with the line
For Each frm As Form In Main.MdiChildren.AsEnumerable

Open in new window

, but that method doesn't find any children either.

So, it would seem that I really just need to know how to establish a list of child forms currently shown on the Main form inside flpMain.

Tony GardnerSr. Programmer/AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
It is important to first determine where the error (object not set...) is happening. Check that first before just making random changes and hoping.
Tony GardnerSr. Programmer/AnalystAuthor Commented:
Thanks for jumping in, Andy. Of course, you are entirely correct, but as you might have guessed, I really had no idea what the error message meant at all!

I've decided to temporarily shelve this task until I can complete another -- that is, I've changed my approach to adding the ToolStripMenuItem when the form is shown and Main is assigned as the MDI parent. So, when the user clicks "Show Form 1", a menu option would be added to the Close Forms sub-menu DropDownItem called "Close Form 1". If I can pull that off, the rest should fall into place.

In the meantime, if anyone on Experts Exchange is familiar with adding and removing ToolStripMenuItems, any assistance would certainly be appreciated. For now, I found an interesting article that's close to this subject on and will post an update once I've sorted through that.
AndyAinscowFreelance programmer / ConsultantCommented:
>>In the meantime, if anyone on Experts Exchange is familiar with adding and removing ToolStripMenuItems, any assistance would certainly be appreciated.

You would be better opening another question about that.

With your error message do you know about using breakpoints and stepping through code?
Tony GardnerSr. Programmer/AnalystAuthor Commented:
The Official Answer for this Question is...


To be specific, .NET does not support placing forms inside of a FlowLayoutPanel because the form class is a Top Level Control.

The only  alternative to this is to utilize the MDI parent container to house the various children and attempt to simulate the FLP behavior by also specifying the child form's .Location property by cycling through the currently shown forms in the Main MDI parent.

I will post a separate question if necessary to reach out for assistance with managing the ToolStripMenuItem collection, but for the moment it appears to be stable and operational for the most part.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.