Title | Keep track of the number of MDI child forms |
Keywords | MDI, subforms |
Categories | Controls, Software Engineering |
|
|
When an MDI child form loads or unloads, look through the Forms collection checking the MDIChild property.
The following code runs in a subform that may or may not be an MDI child form.
|
|
Private Sub Form_Load()
MDIForm1.CountMDIChildren False
End Sub
Private Sub Form_Unload(Cancel As Integer)
MDIForm1.CountMDIChildren True
End Sub
|
|
The following code runs in the MDI parent form named MDIForm1. Notice how it subtracts 1 from the MDI child form count if a child form is unloading. In that case, the child form is still in the Forms collection so it will be counted otherwise.
|
|
Public Sub CountMDIChildren(ByVal one_leaving As Boolean)
Dim frm As Form
Dim is_child As Boolean
Dim num_mdi As Integer
For Each frm In Forms
' See if this form is an MDI child.
On Error Resume Next
is_child = frm.MDIChild
If Err.Number <> 0 Then is_child = False
On Error GoTo 0
If is_child Then num_mdi = num_mdi + 1
Next frm
If one_leaving Then num_mdi = num_mdi - 1
lblNumForms.Caption = Format$(num_mdi)
End Sub
|
|
You can also simply have the child forms update a globally available counter variable when they load and unload.
|
|
|
|