troubleshooting Question

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

Avatar of Tony Gardner
Tony GardnerFlag for United States of America asked on
Visual Basic.NET
4 Comments1 Solution108 ViewsLast Modified:
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

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

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))
with the line
For Each frm As Form In Main.MdiChildren.AsEnumerable
, 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!
ASKER CERTIFIED SOLUTION
Tony Gardner
Sr. Programmer/Analyst

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros