dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Unhandled exception on form closing

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Location
    Memphis, TN
    Posts
    24

    Resolved [RESOLVED] Unhandled exception on form closing

    I have a simple form that when filled out with a case # will return the name of the person that is handling the case. I am getting an error on any machine that does not have VS 2010 installed on it. The error is when ever the user uses the close button (the red X) at the top right of the form the following error is generated:
    Code:
    System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
       at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
       at System.Collections.Generic.List`1.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 Case_Info_Application.Form1.Dispose(Boolean disposing)
       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)
    Here is the code for the form itself:
    Code:
    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            Dim user As String = logon.user
            Dim pword As String = logon.pword
    
            Dim con As New SqlConnection(...)
    
    
            Dim i As Integer = 1
            Dim casenumbers As String
    
            If InStr(TextBox1.Text, "ME") Then
                casenumbers = TextBox1.Text
            Else
                casenumbers += "', '" & TextBox1.Text & "ME"
            End If
            If TextBox2.Text <> "" Then
                If InStr(TextBox2.Text, "ME") Then
                    casenumbers += "', '" & TextBox2.Text
                    i += 1
                Else
                    casenumbers += "', '" & TextBox2.Text & "ME"
                    i += 1
                End If
            End If
            If TextBox3.Text <> "" Then
                If InStr(TextBox3.Text, "ME") Then
                    casenumbers += "', '" & TextBox3.Text
                    i += 1
                Else
                    casenumbers += "', '" & TextBox3.Text & "ME"
                    i += 1
                End If
            End If
            If TextBox4.Text <> "" Then
                If InStr(TextBox4.Text, "ME") Then
                    casenumbers += "', '" & TextBox4.Text
                    i += 1
                Else
                    casenumbers += "', '" & TextBox4.Text & "ME"
                    i += 1
                End If
            End If
    
    
    
    
    
            Dim sql As New SqlCommand(command, con)
            Try
                con.Open()
                Dim reader As SqlDataReader
                reader = sql.ExecuteReader
                Dim sArray(i - 1) As String
                Dim x As Integer = 0
                While reader.Read
    
                    sArray(x) = reader.Item(1).ToString
                    x += 1
                End While
                Label1.Text = sArray(0)
                Label1.Show()
                If TextBox2.Text <> "" Then
                    Label2.Text = sArray(1)
                    Label2.Show()
                End If
                If TextBox3.Text <> "" Then
                    Label3.Text = sArray(2)
                    Label3.Show()
                End If
                If TextBox4.Text <> "" Then
                    Label4.Text = sArray(3)
                    Label4.Show()
                End If
    
    
                casenumbers = Nothing
            Catch ex As Exception
                MsgBox(ex)
            Finally
                con.Close()
                con.Dispose()
                sql.Dispose()
            End Try
    
    
        End Sub
    
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Label1.Hide()
            Label2.Hide()
            Label3.Hide()
            Label4.Hide()
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Try
                Me.Close()
            Catch ex As Exception
                MsgBox(ex)
            End Try
    
    
        End Sub
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            Me.Close()
    
        End Sub
    End Class
    The only collection that I see is the string array (sArray). I read that this issue occurs when the list is changed while being looped through but I don't see that here. Any Help?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,574

    Re: Unhandled exception on form closing

    I don't see it either, and I'm not sure that it would or could happen with an array anyways.

    I see a mention of VBPowerPacks. Does that mean anything to you? Are you using something from that? It certainly sounds like something isn't getting copied correctly on installations, and I have some vague memory of people having other issues about this when using VBPowerPacks incorrectly.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Location
    Memphis, TN
    Posts
    24

    Re: Unhandled exception on form closing

    So just in case someone else has this problem here is the solution. For some reason the forms were not disposing of the shape collections correctly.
    Option one is to remove anything that uses the visual basic power pack 10.0.
    Option two is to insert this function at the top of the form designer code where the dispose sub is called. Make sure you adjust the statement for all of your shape containers. (shapecontainer1, shapecontainer2, etc..)
    Code:
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing Then DisposeShapeContainer(ShapeContainer1)
            If disposing Then DisposeShapeContainer(ShapeContainer2)
            If disposing Then DisposeShapeContainer(ShapeContainer3) ', etc...
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
    Protected Sub DisposeShapeContainer(ByVal AShapeContainer As Microsoft.VisualBasic.PowerPacks.ShapeContainer)
        If AShapeContainer IsNot Nothing Then
            If AShapeContainer.Shapes IsNot Nothing Then
                Dim tshapes As New Generic.List(Of Microsoft.VisualBasic.PowerPacks.Shape)
                For Each tshape As Microsoft.VisualBasic.PowerPacks.Shape In AShapeContainer.Shapes
                    tshapes.Add(tshape)
                Next
                AShapeContainer.Shapes.Clear()
                AShapeContainer.Shapes.Dispose()
                For Each tshape As Microsoft.VisualBasic.PowerPacks.Shape In tshapes
                    tshape.Dispose()
                Next
            End If
            AShapeContainer.Dispose()
        End If
    End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2011
    Location
    Memphis, TN
    Posts
    24

    Re: [RESOLVED] Unhandled exception on form closing

    Here's where I found the information I just posted:
    http://channel9.msdn.com/forums/Tech...Shape-Dispose/

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,574

    Re: [RESOLVED] Unhandled exception on form closing

    Good that you solved it, and thanks for posting the solution.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width