How can i loop through mdi child forms? Code please

  • Thread starter Thread starter Edwinah63
  • Start date Start date
E

Edwinah63

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
 
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
 
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

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

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
 
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
 
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

">
 
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
 
Cor Ligthert said:
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
 
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
 
Back
Top