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

Tony Gardner
Tony Gardner used Ask the Experts™
on
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.

THE QUESTION:
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"
               frm.Close()
       End Select
   Next
   GC.Collect()
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
            Try
                ControlList.Add(child)
            Catch
            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
  HResult=-2147467261
  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.

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
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/Analyst

Author

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 CodeProject.com and will post an update once I've sorted through that.
AndyAinscowFreelance programmer / Consultant

Commented:
>>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?
Sr. Programmer/Analyst
Commented:
The Official Answer for this Question is...

"YOU CAN'T DO THAT!!!"

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.

Cheers!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial