?
Solved

Closing all open child forms.

Posted on 2016-11-28
4
Medium Priority
?
96 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 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 64

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 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