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.
Just using a close command produces the same ObjectDisposedException error
Hi @Hugh Self Taught ,
Could you share a sample on onedrive or github? It will help us make a test and reproduce your problem.
s!AjHClnMNBFXJizxQ7h7PCqmWmqyc
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
Hi @Hugh Self Taught ,
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?
Morning, I rem'd out the Dispose in the form leaving the one in the designer & I still got the error. See screenshot
s!AjHClnMNBFXJiz3Xc8EuHwHQJ4n3
I then put un-rem'd the procedure on the form & rem'd out the one in the designer & got this error
s!AjHClnMNBFXJiz6-ZM75n7iOgE0k
After that I took out this line in the close event DisposeShapeContainer(ShapeContainer1) just calling 'Dispose'
but still got the disposed exception error.
In the interim, I've placed a 0 bordersize button with same backcolor over the rectangle sufficiently so only the rectangle's border is visible. The normal winforms button calling 'dispose' closes the form. Whether it's releasing the resources correctly, I have no idea.
Hi @Hugh Self Taught ,
Thanks for your feedback.
In order to better analyze the question, could you provide a simple example that can help us reproduce the problem?
Hi there, Hopefully you can access the following onedrive zipped video of following breakpoints
s!AjHClnMNBFXJiz8-3TgvA4aqO7rn
Hi @Hugh Self Taught
Unfortunately, this MP4 still can't help us reproduce the problem.
Could you share your project (excluding all private information) here?
Sign in to comment
Activity