VB.net Collection was modified; enumeration operation may not execute Error

1.5k views Asked by At

I'm getting a Collection was modified; enumeration operation may not execute error every time I try to close this form. I suspect it has something to do with the StringBuilder() which I have declared in the starting form (Details1). I have already spent hours looking for the problem. It does not throw an error when I debug, only when I install on other machines.

Please help.

> Public Class PLocation
> 
>     Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
>         Label2.Text = ""
>         Dim showForm As Form = My.Settings.PreviousForm
>         showForm.Show()
>         Me.Hide()
>     End Sub
> 
>     Private Sub PLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
>         Label2.Text = ""
>         Label1.Text = My.Settings.CurrentProject1
>         Label6.Text = My.Settings.UserInitials1
>         Label5.Text = My.Settings.CurrentDataPath1
>         Label7.Text = My.Settings.EquipID1
>         Label8.Text = My.Settings.CurrentPunchItem1
>         Home.Details1.Clear()
> 
>     End Sub
> 
>     Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
>         Dim ExitBox As MsgBoxResult = MessageBox.Show("Are you sure you want to CLOSE this program?", _
>           "WARNING", _
>           MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
> 
>         'If Yes, then delete
>         If ExitBox = MsgBoxResult.Yes Then
>             Home.Close()
> 
>         End If
>     End Sub
> 
>     
> 
>     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
>         If TextBox1.Text.Contains(",") Then
>             Label2.Text = "Remove commas in comment box."
>         Else
>             Label2.Text = ""
>             If CheckBox1.Checked Then
>                 Home.Details1.Append("Breakover; ")
>             End If
> 
>             If CheckBox2.Checked Then
>                 Home.Details1.Append("Chute; ")
>             End If
> 
>             If CheckBox3.Checked Then
>                 Home.Details1.Append("Drive pulley; ")
>             End If
> 
>             If CheckBox4.Checked Then
>                 Home.Details1.Append("End pulley; ")
>             End If
>             If CheckBox5.Checked Then
>                 Home.Details1.Append("Return roller; ")
>             End If
>             If CheckBox6.Checked Then
>                 Home.Details1.Append("Sideguard; ")
>             End If
>             If CheckBox7.Checked Then
>                 Home.Details1.Append("Slider bed; ")
>             End If
>             If CheckBox8.Checked Then
>                 Home.Details1.Append("Snub; ")
>             End If
>             If CheckBox9.Checked Then
>                 Home.Details1.Append("Take-up; ")
>             End If
>             If CheckBox10.Checked Then
>                 Home.Details1.Append("Head end; ")
>             End If
>             If CheckBox11.Checked Then
>                 Home.Details1.Append("Tail end; ")
>             End If
>             If CheckBox12.Checked Then
>                 Home.Details1.Append("Drive section; ")
>             End If
>             If CheckBox13.Checked Then
>                 Home.Details1.Append("Middle; ")
>             End If
>             If CheckBox14.Checked Then
>                 Home.Details1.Append("Left side; ")
>             End If
>             If CheckBox15.Checked Then
>                 Home.Details1.Append("Right side; ")
>             End If
>             If CheckBox16.Checked Then
>                 Home.Details1.Append("at joint ; ")
>             End If
>             If CheckBox17.Checked Then
>                 Home.Details1.Append("Top side; ")
>             End If
>             If CheckBox18.Checked Then
>                 Home.Details1.Append("Bottom side; ")
>             End If
>             If CheckBox19.Checked Then
>                 Home.Details1.Append("Insider / Inner; ")
>             End If
>             If CheckBox20.Checked Then
>                 Home.Details1.Append("Outside / Outer; ")
>             End If
>             If CheckBox21.Checked Then
>                 Home.Details1.Append("Above; ")
>             End If
>             If CheckBox22.Checked Then
>                 Home.Details1.Append("Below; ")
>             End If
>             If CheckBox23.Checked Then
>                 Home.Details1.Append("Lower; ")
>             End If
>             If CheckBox24.Checked Then
>                 Home.Details1.Append("Upper; ")
>             End If
>             If CheckBox27.Checked Then
>                 Home.Details1.Append("Bad installation; ")
>             End If
>             If CheckBox28.Checked Then
>                 Home.Details1.Append("Bent; ")
>             End If
>             If CheckBox29.Checked Then
>                 Home.Details1.Append("Damaged; ")
>             End If
>             If CheckBox30.Checked Then
>                 Home.Details1.Append("Dented; ")
>             End If
>             If CheckBox31.Checked Then
>                 Home.Details1.Append("Gap; ")
>             End If
>             If CheckBox32.Checked Then
>                 Home.Details1.Append("Incomplete; ")
>             End If
>             If CheckBox33.Checked Then
>                 Home.Details1.Append("Insufficient; ")
>             End If
>             If CheckBox34.Checked Then
>                 Home.Details1.Append("Loose; ")
>             End If
>             If CheckBox35.Checked Then
>                 Home.Details1.Append("Misaligned; ")
>             End If
>             If CheckBox36.Checked Then
>                 Home.Details1.Append("No access; ")
>             End If
>             If CheckBox37.Checked Then
>                 Home.Details1.Append("Uneven; ")
>             End If
>             If CheckBox38.Checked Then
>                 Home.Details1.Append("Not installed; ")
>             End If
>             If CheckBox39.Checked Then
>                 Home.Details1.Append("Not straight; ")
>             End If
>             If CheckBox40.Checked Then
>                 Home.Details1.Append("Rusted; ")
>             End If
>             If CheckBox41.Checked Then
>                 Home.Details1.Append("Sharp edges; ")
>             End If
>             If CheckBox42.Checked Then
>                 Home.Details1.Append("Too large; ")
>             End If
>             If CheckBox43.Checked Then
>                 Home.Details1.Append("Too long; ")
>             End If
>             If CheckBox44.Checked Then
>                 Home.Details1.Append("Too short; ")
>             End If
>             If CheckBox45.Checked Then
>                 Home.Details1.Append("Too Small; ")
>             End If
>             If CheckBox46.Checked Then
>                 Home.Details1.Append("Wrong type; ")
>             End If
>             If CheckBox47.Checked Then
>                 Home.Details1.Append("Wrong color; ")
>             End If
>             If CheckBox48.Checked Then
>                 Home.Details1.Append("Wrong size; ")
>             End If
>             If CheckBox25.Checked Then
>                 Home.Details1.Append("times 2; ")
>             End If
>             If CheckBox26.Checked Then
>                 Home.Details1.Append("times 3; ")
>             End If
>             If TextBox1.Text = "" Then
>                 'do nothing
>             Else
>                 Home.Details1.Append(", " & TextBox1.Text)
>             End If
> 
>             Home.Details2 = Home.Details1.ToString
>             Summary.Show()
>             Me.Close()
> 
>         End If
>     End Sub
> 
>     
> 
>     Private Sub Label16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
>         About.Show()
>     End Sub
> 
> 
> End Class
> 

***** Exception Text ******* System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List1.Enumerator.MoveNextRare() at System.Collections.Generic.List1.Enumerator.MoveNext() 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.Windows.Forms.Form.Dispose(Boolean disposing) at WindowsApplication1.PLocation.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)

1

There are 1 answers

0
John Hart On

This is a strange problem. I can't determine what might be causing it just from the information you've posted. I also wasn't able to reproduce the issue here either. It does look like you have a lot of controls on your form, not that would be the issue either necessarily but it may be contributing.

If you would like to, feel free to send me an email directly John.Hart @ Microsoft.com and we’ll take this off line to see if we can get to the bottom of it.

Thanks, John