Error being thrown in Win 8.1 but not Win 7 nor Vistsa. Appears to be form closing event.

I have an app that is causing errors to be thrown on a couple of forms during the closing event. Th error is only showing up on Windows 8.1. Win 7 and Vista are not throwing the error.

Is this bad code where the older OS's were forgiving me or is it something else? The APP is in VB.net 2008. I can click continue on the error and the APP keeps running as intended.

Unfortunately my debugging is limited because I do not have Win 8.1.

    Private Sub Form7_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim frm As Form
        For Each frm In Me.MdiParent.MdiChildren
            If TypeOf frm Is Form2 Or TypeOf frm Is Form3 Or TypeOf frm Is Form4 Or TypeOf frm Is Form5 Or TypeOf frm Is Form6 Or TypeOf frm Is Form8 Or TypeOf frm Is Form9 Or TypeOf frm Is Form10 Or TypeOf frm Is Form11 Or TypeOf frm Is Form12 Or TypeOf frm Is Form13 Or TypeOf frm Is Form14 Or TypeOf frm Is Form15 Or TypeOf frm Is Form16 Or TypeOf frm Is Form17 Or TypeOf frm Is Form18 Or TypeOf frm Is Form19 Or TypeOf frm Is Form20 Or TypeOf frm Is Form21 Or TypeOf frm Is Form22 Or TypeOf frm Is Form23 Or TypeOf frm Is Form24 Or TypeOf frm Is Form25 Or TypeOf frm Is Form26 Or TypeOf frm Is Form27 Or TypeOf frm Is Form28 Then
                frm.Close()
            End If
        Next
    End Sub

Open in new window

PBLackAsked:
Who is Participating?
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
And you say it worked before? I focused on that statement and did not look further. But with the error message, it becomes evident.

You cannot remove elements from a collection when you loop with a for each. You have to do it in reverse with the "standard" for, replacing each instance of frm by Me.MdiParent.MdiChildren(x):

For x As Integer = Me.MdiParent.MdiChildren.Count-1 to 0 Step -1
     If TypeOf Me.MdiParent.MdiChildren(x) Is Form2 ... Then
          Me.MdiParent.MdiChildren(x).Close()
     End If
Next
0
 
Haris DjulicCommented:
Can you post error raised?
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
If we do not know what the error is, we cannot answer.

Also, is it always happening on the same forms. Calling Close on these forms will also trigger the FormClosing error on them, so it might be where you have a problem. The code that you are showing should usually not give problems by itself.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
PBLackAuthor Commented:
Hold on I will get them to send me a screen shot
0
 
PBLackAuthor Commented:
Message from error box

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at Microsoft.VisualBasic.PowerPacks.ShapeCollection.Dispose(Boolean disposing)
   at Microsoft.VisualBasic.PowerPacks.ShapeContainer.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at System.Windows.Forms.Control.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at System.Windows.Forms.Control.Dispose(Boolean disposing)
   at System.Windows.Forms.TabControl.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at System.Windows.Forms.Control.Dispose(Boolean disposing)
   at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
   at System.Windows.Forms.Form.Dispose(Boolean disposing)
   at comstock_Compiler.Form7.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at System.Windows.Forms.Form.WmClose(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.8009 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
comstock Compiler
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Program%20Files%20(x86)/comstock/comstock%20Compiler/comstock%20Compiler.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.8007 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.7905 (win9rel.050727-7900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.8008 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.8007 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.8007 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.8009 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.8003 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
OpenNETCF.Desktop.Communication
    Assembly Version: 2.9.2244.24857
    Win32 Version: 2.9.2244.24857
    CodeBase: file:///C:/Program%20Files%20(x86)/comstock/comstock%20Compiler/OpenNETCF.Desktop.Communication.DLL
----------------------------------------
System.Data.SqlServerCe
    Assembly Version: 3.5.1.0
    Win32 Version: 3.5.8080.0
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Data.SqlServerCe/3.5.1.0__89845dcd8080cc91/System.Data.SqlServerCe.dll
----------------------------------------
System.Data
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.8007 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Transactions
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.8007 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.8007 (FX35W81RTMGDR.050727-8000)
    CodeBase: file:///C:/Windows/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
Microsoft.VisualBasic.PowerPacks.Vs
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.20911.1
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.VisualBasic.PowerPacks.Vs/10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.PowerPacks.Vs.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

Open in new window


Screen shot
error.JPG
0
 
PBLackAuthor Commented:
Yes it works 100% of the time on Vista and Win 7 installs. Today was the first time it was installed on Win 8.1. I will make the change you suggested and issue a fix, just in case.
0
 
PBLackAuthor Commented:
I closed the question too soon.

Me.MdiParent.MdiChildren.Count -  Error: 'Count' is not a member of 'System.Array'
0
 
Haris DjulicCommented:
Try changing the For to :

For x = 0 To (Me.MdiChildren.Length) - 1
0
 
PBLackAuthor Commented:
@samo4fun - thank you
0
 
Haris DjulicCommented:
Just thank you? Kidding ;)   glad to help ...
0
 
PBLackAuthor Commented:
Well I wish I could give you some points but stupid me closed it.
0
 
Haris DjulicCommented:
No biggie...
0
All Courses

From novice to tech pro — start learning today.