次のコードは、タグを使用せずに必要なことを行います。
Public Class Form1
Private ChildNumber As Integer = 0
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim frm As New Form1
ChildNumber += 1
frm.Text = "Child Form " & ChildNumber
frm.Show()
frm.TopLevel = False
Me.Controls.Add(frm)
frm.Tag = "temptag"
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim count As Integer = Me.Controls.Count
For i As Integer = Me.Controls.Count - 1 to 0 Step -1
Dim ctrl As Control
ctrl = Me.Controls(i)
If ctrl.Tag = "temptag" Then
DirectCast(ctrl, Form1).Dispose()
End If
Next
End Sub
End Class