question

HughNaude-7306 avatar image
0 Votes"
HughNaude-7306 asked XingyuZhao-MSFT commented

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

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.

dotnet-visual-basic
· 11
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Does it work if you do not dispose anything?


0 Votes 0 ·

Just using a close command produces the same ObjectDisposedException error

0 Votes 0 ·

Hi @HughNaude-7306 ,
Could you share a sample on onedrive or github? It will help us make a test and reproduce your problem.

0 Votes 0 ·

Hi, Thanks for trying to assist. I've zipped the main menu form as well as the other form that calls the addeventmutiple form which is the one I'm trying to close. As mentioned I'm using Powerpack3

0 Votes 0 ·

Hi @HughNaude-7306 ,

If I try to compile I get the error that Protected Overides Sub Dispose(Disposing as Boolean) has multiple definitions with identical signatures

You both have 'Dispose(ByVal disposing As Boolean)' method in 'frmAddEventMultiple.vb' and 'frmAddEventMultiple.Designer.vb', you need to delete one of them.

Besides, if you do not dispose ‘ShapeContainer’ explicitly, will the exception also occur?

0 Votes 0 ·
Show more comments

0 Answers