I have worked with client's applications that have MDI forms and their application cannot be modified otherwise, so support for the MDI pattern must be maintained. So I hope this helps.
I would look into all the form’s members that deal with MDI forms. Two that are useful are:
ActiveMdiChild
MDIChildren
These two can solve a lot of problems. Also, you can write code to facilitate this, for example; in sub ToolStripColorToRed_Click I use the form’s ActiveMdiChild property and in sub ToolStripColorToBlue_Click I used my code that is supported by the MDI and specific class frmChild.
Public Class Form1
Delegate Sub ActiveFormDel(ByVal sender As Object, ByVal e As EventArgs)
Delegate Sub FormSaysGoodByDel(ByVal sender As Object, ByVal e As EventArgs)
Dim _formCount As Integer
Dim _childForm As frmChild
Private Sub ToolStripNewChildForm_Click(sender As Object, e As EventArgs) Handles ToolStripNewChildForm.Click
Dim frm As New frmChild("New Child Form - " & _formCount.ToString,
AddressOf SetActiveForm, AddressOf GoodByMDIParent)
frm.MdiParent = Me
frm.Show()
_formCount += 1
End Sub
Private Sub SetActiveForm(ByVal sender As Object, ByVal e As EventArgs)
Try
_childForm = DirectCast(sender, frmChild)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub GoodByMDIParent(ByVal sender As Object, ByVal e As EventArgs)
Dim frm As frmChild = DirectCast(sender, frmChild)
If frm Is _childForm Then
_childForm = Nothing
End If
_formCount -= 1
End Sub
Private Sub ToolStripColorToRed_Click(sender As Object, e As EventArgs) Handles ToolStripColorToRed.Click
If Me.ActiveMdiChild IsNot Nothing Then
ActiveMdiChild.BackColor = Color.Red
End If
End Sub
Private Sub ToolStripColorToBlue_Click(sender As Object, e As EventArgs) Handles ToolStripColorToBlue.Click
If _childForm IsNot Nothing Then
_childForm.BackColor = Color.Blue
End If
End Sub
End Class
Public Class frmChild
Dim SetActive As Form1.ActiveFormDel
Dim SayGoodBy As Form1.FormSaysGoodByDel
Public Sub New(ByVal name As String, ByVal del As Form1.ActiveFormDel, ByVal del2 As Form1.FormSaysGoodByDel)
SetActive = del
SayGoodBy = del2
' This call is required by the designer.
InitializeComponent()
Text = name
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub frmChild_Activated(sender As Object, e As EventArgs) Handles Me.Activated
SetActive(Me, New EventArgs)
End Sub
Private Sub frmChild_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
SayGoodBy(Me, New EventArgs)
End Sub
End Class
Regs,
Ron O.