Solved

Closing all open child forms.

Posted on 2016-11-28
4
24 Views
Last Modified: 2016-11-29
Hello,
i am looking forward to close all the child forms beginnig with 'FrmInvoiceDetails' but,i get exception , collection changed.


  For Each form In My.Application.OpenForms
                        'MessageBox.Show(form.name)
                        If form.Name.StartsWith("FrmInvoiceDetails") Then
                            If form.Visible Then
                                form.close
                            End If
                            ' IntFormNumber += 1
                        End If

                    Next

Open in new window



Cheers
0
Comment
Question by:RIAS
  • 2
4 Comments
 
LVL 19

Expert Comment

by:darbid73
ID: 41904237
You have a collection of Opens forms in your loop.  As soon as you close one you are affecting the collection and thus the loop.

You need to use a revers for loop -1.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41904243
Hi RIAS;
 
Not at my computer but this should work. Let me know if you have issues with it.
For index As Integer = My.Application.OpenForms.Count - 1 To 0
    If My.Application.OpenForms(index).Name.StartsWith("FrmInvoiceDetails") Then
        If My.Application.OpenForms(index).Visible Then
            My.Application.OpenForms(index).Close
        End If 
        ' IntFormNumber += 1
    End If
Next 

Open in new window

0
 
LVL 19

Expert Comment

by:darbid73
ID: 41904245
It seems I cannot edit my comment any more.  Sorry I hit submit too quickly.

For i = (My.Application.OpenForms.Count - 1) To 0 Step -1
	'get a form here
	'now check and close it.   
Next i

Open in new window

0
 

Author Closing Comment

by:RIAS
ID: 41905476
Cheers!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now