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
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
, 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
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?