System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'ShapeContainer'.'

Hugh Self Taught 81 Reputation points
2021-07-12T10:19:21.227+00:00

Hi Gurus, I'm using the vb powerpack v3 as I believe there's an issue with v10 in vs2019. I've used the Rectangle shape to create decent looking buttons on a form. I've used this code to at text to it

    Private Sub btnRectangleClose_Paint(sender As Object, e As PaintEventArgs) Handles btnRectangleClose.Paint
        Dim s As String = "Close"
        Dim f As Font = Me.Font
        Dim p As New Point(btnRectangleClose.Location.X + 15, btnRectangleClose.Location.Y + 5)
        Dim g As Graphics = e.Graphics
        If btnCloseVis Then
            btnRectangleClose.BackColor = Color.SandyBrown
            btnRectangleClose.BorderColor = Color.DarkRed
            g.DrawString(s, f, Brushes.Black, p)
        Else
            btnRectangleClose.BackColor = Color.PeachPuff
            btnRectangleClose.BorderColor = Color.IndianRed
            g.DrawString(s, f, Brushes.LightGray, p)
        End If
    End Sub

If I use a normal vs button to Dispose the form it closes correctly but if I use the click event of the Rectangle I get the error
System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'ShapeContainer'

I then found the following was required when disposing of PowerPack shapes

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            DisposeShapeContainer(ShapeContainer1)
            'DisposeShapeContainer(ShapeContainer2)
        End If

        If (disposing _
                    AndAlso (Not (components) Is Nothing)) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
        'ShapeContainer1(disposing)
    End Sub

    Protected Sub DisposeShapeContainer(ByVal AShapeContainer As Microsoft.VisualBasic.PowerPacks.ShapeContainer)
        If (Not (AShapeContainer) Is Nothing) Then
            If (Not (AShapeContainer.Shapes) Is Nothing) Then
                Dim tshapes As List(Of Microsoft.VisualBasic.PowerPacks.Shape) = New 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

If I try to compile I get the error that Protected Overides Sub Dispose(Disposing as Boolean) has multiple definitions with identical signatures and refers to this code in the designer with 'Dispose' indicated

Partial Class frmAddEventMultiple
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

If I take that code out & call DisposeShapeContainer(ShapeContainer1) as I read elsewhere then I get the System.ObjectDisposedException error
I'm at a loss as to how to resolve this. Any assistance greatly appreciated.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
{count} votes