By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

How can i loop through mdi child forms? Code please

P: n/a
Hi Everyone,

in vb6 i was able to execute the following code and it would close the
children is the reverse order they were opened eg the last child
opened was the first child to close.

in mdiparent i had this code

i = Forms.Count - 1
Do Until i = 0
If Forms(i).Name <> "mdiMain" Then Unload(Forms(i))
i = i - 1
Loop

in the queryunload event of each child form i would prompt the user
if msgbox(msg) = vbyes then
'clean up objects etc
else
cancel = true
end if

setting cancel to true, seemed to kill the loop in the code in
mdiParent.

i have tried using in the new vbnet mdiParent form:

Dim f As Form
For Each f In Me.MdiChildren
if Not IsNothing(f) Then f.Close()

'somehow capture if the user has
'cancelled the close of a child form and exit loop

Next

in the closing event of each child
if msgbox(msg) = vbyes then
'clean up objects etc
else
e.cancel = true
end if

the forms seem to close in the order they are opened, however i would
prefer them to close in reverse order.

also, how can i capture if a user has canceled the form close event?
is there something already existing that i can check, i am not a fan
of setting global flags.

ideally, i would like the vb.net code equivalent of my old vb6 code

many thanks

Edwinah63
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Edwinah,

What is that sentence "clean up objects", when you have done everything as
it should be, than that is done by the framework. That is one of the reasons
why it is managed code.

Maybe this helps already something?

Cor
Nov 21 '05 #2

P: n/a
Hi,

Try something like this.

Dim f() As Form = Me.MdiChildren

For x As Integer = f.GetUpperBound(0) To 0 Step -1

f(x).Close()

Next

Ken

-------------------

"Edwinah63" <ed*****@customercare.com.au> wrote in message
news:d7**************************@posting.google.c om...
Hi Everyone,

in vb6 i was able to execute the following code and it would close the
children is the reverse order they were opened eg the last child
opened was the first child to close.

in mdiparent i had this code

i = Forms.Count - 1
Do Until i = 0
If Forms(i).Name <> "mdiMain" Then Unload(Forms(i))
i = i - 1
Loop

in the queryunload event of each child form i would prompt the user
if msgbox(msg) = vbyes then
'clean up objects etc
else
cancel = true
end if

setting cancel to true, seemed to kill the loop in the code in
mdiParent.

i have tried using in the new vbnet mdiParent form:

Dim f As Form
For Each f In Me.MdiChildren
if Not IsNothing(f) Then f.Close()

'somehow capture if the user has
'cancelled the close of a child form and exit loop

Next

in the closing event of each child
if msgbox(msg) = vbyes then
'clean up objects etc
else
e.cancel = true
end if

the forms seem to close in the order they are opened, however i would
prefer them to close in reverse order.

also, how can i capture if a user has canceled the form close event?
is there something already existing that i can check, i am not a fan
of setting global flags.

ideally, i would like the vb.net code equivalent of my old vb6 code

many thanks

Edwinah63
Nov 21 '05 #3

P: n/a
> What is that sentence "clean up objects"

sorry Cor, this is just my own user made objects, not system objects
etc i prefer to explicitly extinguish user made objects rather than
relying on them to just go out of scope.

However, does anyone know how i can capture if the user has cancelled
the close of a child form?

all thoughts appreciated. many thanks to Ken for his code.

Edwinah63
Nov 21 '05 #4

P: n/a
Edwinah,

It should not be necassary however maybe you can use this.

\\\
For Each frm As Form In Me.MdiChildren
' do cleanup
Next
///

I hope this helps?

Cor

">
sorry Cor, this is just my own user made objects, not system objects
etc i prefer to explicitly extinguish user made objects rather than
relying on them to just go out of scope.

However, does anyone know how i can capture if the user has cancelled
the close of a child form?

all thoughts appreciated. many thanks to Ken for his code.

Edwinah63

Nov 21 '05 #5

P: n/a
Does anyone know how to capture if a user has cancelled the close event on a form?

i am not sure that question was answered.

ta
Nov 21 '05 #6

P: n/a
Did you see my answer?

When a form is in that loop it is not closed.

Cor
Nov 21 '05 #7

P: n/a
"Cor Ligthert" <no**********@planet.nl> wrote in message news:<uO*************@TK2MSFTNGP12.phx.gbl>...
Did you see my answer?

When a form is in that loop it is not closed.

Cor


Hi Cor,

i did see your answer:

For Each frm As Form In Me.MdiChildren
' do cleanup
Next

but i am not sure it is applicable here.
i am currently using Ken's code in my app:

Dim f() As Form = Me.MdiChildren

For x As Integer = f.GetUpperBound(0) To 0 Step -1

f(x).Close()

Next
What i am after is a way of stopping the loop if a user cancels an
explicit close eg:

Dim f() As Form = Me.MdiChildren

For x As Integer = f.GetUpperBound(0) To 0 Step -1

f(x).Close()

if UserCancelsChildFormClose then
'exit for or do something else
end if

Next

i can achieve this by setting a global varible but i was wondering if
there is something native in the forms that i can detect (a cancel
event???).

many thanks

Edwinah63
Nov 21 '05 #8

P: n/a
Edwinah,

In every close event from a form is the e.cancel, you can set that to true
or false and I am never sure if it is e.cancel = true means cancel the close
operation or that it means e.cancel is true close the class, so try it
yourself. (I thought it was the first)

Than you can check in that close event if it is done in the correct way or
that the user has pushed the close X in the top.

To get the main form in a mdichild is
me.parent.xxxxx

I hope this helps?

Cor
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.