?
Solved

Closing all open child forms.

Posted on 2016-11-28
4
Medium Priority
?
75 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 20

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 63

Accepted Solution

by:
Fernando Soto earned 2000 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 20

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 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