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
Solved

Closing all open child forms.

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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