For Each MdiChild in Me.MdiChildren??

  • Thread starter Thread starter DraguVaso
  • Start date Start date
D

DraguVaso

Hi,

I have a Form which has the property IsMdiContainer = True. So it contains a
whole bunch of MdiChilds. Those MdiChilds are all instances of two forms I
have: I have some that are an instance of frmSource, and some that are
instances of frmTarget.

I now have a button on my MdiParent that, if clicked on, have to refresh all
the instances of frmSource. So I putted the following code underneath it:
Dim FormSource as frmSource
For Each FormSource In Me.MdiChildren
FormSource.FillGrid_TS()
Next

This works fine when I have ONLY instances of frmSource, but it doesn't work
when I have also instances of frmTarget.

I found alreadyy a solution ofr my problem, but I still think it's a little
bit weird. (see underneath this). Does anydbody know a better/ more corect
solution?

Dim FormSource As frmSource
Dim FormTest As Form
btnRefreshAll.Enabled = False
For Each FormTest In Me.MdiChildren
If FormTest.Name = "frmSource" Then
FormSource = FormTest
FormSource.FillGrid_TS()
End If
Next

Thanks a lot in advance!

Pieter
 
DraguVaso said:
Dim FormSource As frmSource
Dim FormTest As Form
btnRefreshAll.Enabled = False
For Each FormTest In Me.MdiChildren
If FormTest.Name = "frmSource" Then
FormSource = FormTest
FormSource.FillGrid_TS()
End If
Next


Option Strict On
'...

Dim FormSource As frmSource
Dim FormTest As Form
btnRefreshAll.Enabled = False
For Each FormTest In Me.MdiChildren
If TypeOf FormTest Is frmSource Then
FormSource = Directcast(FormTest, frmSource)
FormSource.FillGrid_TS()
End If
Next
 
* "DraguVaso said:
I found alreadyy a solution ofr my problem, but I still think it's a little
bit weird. (see underneath this). Does anydbody know a better/ more corect
solution?

Dim FormSource As frmSource
Dim FormTest As Form
btnRefreshAll.Enabled = False
For Each FormTest In Me.MdiChildren
If FormTest.Name = "frmSource" Then

I would prefer 'If TypeOf FormTest Is frmSource Then'.
 
The issue here is that you did not check to see what type of form you have
inside your loop. The MdiChildren collection contains ALL MdiChildren, so
your loop enumerator must be of type Form and then inside the loop, check to
see what specific type of form you have:

Dim countSource As Integer
Dim countTarget As Integer
Dim child As Form

For Each child In MdiChildren

If TypeOf child Is Source Then
countSource += 1
ElseIf TypeOf child Is Target Then
countTarget += 1
End If

Next

MessageBox.Show(countSource.ToString() & " Source Forms" & _
ControlChars.CrLf & _
countTarget.ToString() & " Target Forms")

However, I think that a better design will be more successful. Consider
making your own collections for the different types of forms. In your
MdiParent form, do something like this:

Private Sources As New Collection
Private Targets As New Collection

Private Function getNewSource() As Source
Dim f As New Source()
f.MdiParent = Me
Sources.Add(f)
Return f
End Function

Private Function getNewTarget() As Target
Dim f As New Target()
f.MdiParent = Me
Targets.Add(f)
Return f
End Function

Then you could use code like this to walk over whichever collection you
choose:

Dim f As Form

For Each f In Sources
'Do something to each Source form
Next

If your application creates these two types of forms from say, two different
menu items or whatever (two different event procedures), you could even
refactor the two event procedures into one method of your MdiParent form
that handles both events and decides based on the identity of "sender" which
form to create. This is an example of the "Factory Method Design Pattern"
--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
 
Back
Top