Avatar of Tony Gardner
Tony Gardner
Flag for United States of America asked on

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.

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!
Visual Basic.NET

Avatar of undefined
Last Comment
Tony Gardner

8/22/2022 - Mon
AndyAinscow

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 Gardner

ASKER
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.
AndyAinscow

>>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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Tony Gardner

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.