Solved

Closing all open child forms.

Posted on 2016-11-28
4
45 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 63

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Write to a printer using vb.net 9 30
VB.Net - Get ColumnNames of SQL Table 4 13
.NET 2008 VB and C# 6 28
No Data for DropDown List 2 26
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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