Openforms collection in 1.1 .NetFrameWork??

G

Guest

There appears to be no openforms collections in 1.1 version on the
..NetFramework.

How do I iterate through and close all opened child forms in a Window MDI
application? (In VB.Net although I can usually make out C# code)

WR
 
K

Kevin S Gallagher

Here something I used in 1.1 so I am guessing you could close a form using
this logic

Function IsFormLoaded(ByVal ToBeLoadedForm As String) As Boolean
Dim LoadedForm As Form

Try
For Each LoadedForm In Me.MdiChildren
If LoadedForm.Name = ToBeLoadedForm Then
Return True
Exit For
End If
Next
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & ex.Source & vbCrLf & ex.StackTrace,
MsgBoxStyle.Critical, "Error")
End Try
LoadedForm = Nothing
End Function
 
G

Guest

Thanks for the reply. I am pretty sure you cannot modify the collection
while iterate over it using the for each statement. But you gave this idea
which works.

Dim myforms() As Form = Me.MdiChildren
Dim i As Int16
For i = (myforms.Length - 1) To 0 Step -1
myforms(i).Close()
Next

So it appears that MdiChildren is array of open forms.

Bill
 
K

Kevin S Gallagher

Bill,

Glad to help...Your code is exactly what I was moving you towards :)

Kevin
 
G

Guest

I haven't tried this; but, again, I believe you cannot delete members of a
collection while iterating over it with the for-each statement. That is why
I chose the other solution I posted.
 
J

John

Hi,

The sample I gave you works fine.

It is interesting your point regarding whether or not you can
delete members of a collection while iterating over it with a
for each, I don't have a definative answer for that. Regardless,
that is not what is occurring in this case.

My code does the same thing as yours but is more concise and
more importantly the variables go out of scope after the for
next, thus helping to avoid a memory leak.

The key point here is that MdiChildren returns a new array. In
your code you are working against the array returned by
MdiChildren just like I am. Perhaps what you intended to do
was this:

Dim myforms() As Form
Me.MdiChildren.CopyTo(myforms, 0)
Dim i As Int16
For i = (myforms.Length - 1) To 0 Step -1
myforms(i).Close()
Next

The above is redundant because MdiChildren is already
making a copy of the internal array. Does that make sense or
is my understanding incorrect/incomplete?

Thanks.

J
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top