Hiding MDI forms

K

Kevin

I've got a timer on my MDI parent form. If there's no mouse movement
for a set number of minutes, the Visible property of all open forms is
set to False and the Log On form is displayed. I could do it in VB6.

In VB2005 I can get a list of all the open forms, but I get an error
after hiding the first one.

This is what I'm trying to use:

For Each m In My.Application.OpenForms
If m.Name <> "frmMain" Then
m.Visible = False
End If
Next m

It won't list all the open forms if one of the child forms is showing
another modal form. And again, once it hides the first form, it bombs
out--something about my OpenForms collection changing.
 
C

Cor Ligthert [MVP]

Kevin,

I try to avoid the my namespace, it is mainly created for VB6 developers,
while there were already good solutions, which are in my opinion less
confusing when you want to use OOP. (In some cases the my namespace has
handy features so don't understand me wrong).

In your parent you can use the mdichildren instead of that openforms

http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.mdichildren.aspx

This msdn page is in my idea confusing. It has to be me.mdichildren or just
mdichildren

There are more properties around the MDI form which are easy for you with
what you are busy,

I hope this helps,

Cor
 
K

Kevin

I tried that too. The only problem is, I can't seem to open a
MDIChild form modally. I have to make them non-child forms to open
them modally, so they don't end up in the MDIChildren collection.
 
S

Smokey Grindle

you can't make a child form a modal form, its impossible... modal has to
have top level access and all parents behind it lose access at that time
(they get locked out of gaining focus) this is impossible to do when it is a
child of a MDI form inside a MDI window..
 
S

Smokey Grindle

I bet you anything your exception is because you are modifying the
collection and the enumeration is changing

for each depends on the enumeration it stay constant... you cant change it
during the loop... so when you hide a form its probably removing it from the
enumeration of open forms and its like oh look i change cant keep going...
this also applies to looping through other collection enumerations like list
box items and such and like removing an item from the list while you are
going through it... the only way around it is drop the for each and use a
for i as integer = 0 to my.application.openforms.count -1 type of loop
this will not be affected by the collection changing like for each is
 
G

gene kelley

I started to do the For Next loop, but I'm not real sure how to call
each form.

For Each ChildForm As Form In Me.MdiChildren
ChildForm.Visible = False
Next


Gene
 
S

Smokey Grindle

you can access them by index...

for i as integer = 0 to my.application.openforms.count -1
my.application.openforms(i).visible = false
next
 
K

Kevin

I have MDI forms , but occasionally I have to show a form modally. NOT
EVERY form can be a MDI Child form!
 
K

Kevin

How can I reference them by name? If I put the names of all open forms
into a string array, how can I go back through the list and change
their Visible property back to True?
 
R

R. MacDonald

Hello, Kevin,

I suggest that you try something like the following. Sorry I haven't
tested this because I'm not currently using 2005. :-( Worse, I don't
seem to be able to open MSDN just now to check whether or not OpenForms
has an array nature or a collection nature. If the latter, try changing
my use of "Length" to "Count".

Dim IndexOfNonMainForm As Integer = 0
While Me.MdiChildren.Length > 1
Dim m As Form = MdiChildren(IndexOfNonMainForm)
If (m.Name = "frmMain") Then
IndexOfNonMainForm = 1
m = MdiChildren(IndexOfNonMainForm)
End If
m.Visible = False
End While

Cheers,
Randy
 

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