control over a dynamically created form

Jas91

Member
Joined
Jul 12, 2012
Messages
17
Location
Baghdad, Italy. OK?=
Programming Experience
5-10
hi guys!

I have a method in Form1 which creates instances of Form2 and shows them:
Dim f As New Form2
f.Show()

now I have to use public methods which are declared in Form2, from Form1...I actually tryed this, but didn't work

(I'm on a different method in Form1 now)

For Each frm as Form2 in Application.OpenForms
frm.MyFunction
Next

any idea?
 
You're using .Net 4 so you can use the OfType collection filter method:
For Each frm In Application.OpenForms.OfType(Of Form2)()

The type of frm loop variable is here inferred from the Of T argument, which here is Form2, so the 'As Form2' is not necessary.

Traditionally you would have to use a common type in loop, for example Form, and check the type of object using TypeOf operator, and cast the object to specific type using DirectCast/CType. I mention this because you should know and likely will encounter situations where that is needed later.
 
Back
Top