How to Dispose RectangleShape created on Form1 by button click on Form2

331 views Asked by At

I have Form1 with button which creates some rectangles and another button on same Form1 which deletes those rectangles ( This is working by below code)

canvas.Parent = Nothing

Now In Form1 I have another Button which open New Form2 with some set of values, checkboxes and textboxes

This Form2 has a Checkbox which once checked OK it makes few more rectangles on "FORM1"

Now I need a checkbox/Button on Form2 to reset values and rectangles. So basically when this checkbox is checked it should only delete the rectangle which was made thru this form and not all the rectangles in the background in Form1.

Ex. I already have 5 rectangles in Form1 and when I opened Form2 and entered few values it created 2 more rectangles so Now I have 7 rectangles in Form1. Lets say I dint like the size or orientation of rectangles so I checked 1 box in Form2 and it deleted the last 2 rectangles, so In the end there are 5 rectangles left in Form1. Hope I am clear enough!

I have tried many options but none are working ( ex: I want to delete RectangleShape named "Block" or just RectangleRhape

Block.Dispose()

Block.Invalidate()

Form1.Controls.Find("Block", True)
Block.Dispose()

DirectCast(Controls(dummy), RectangleShape).Dispose()

then I found these below codes which are having problems with "Shapecontainer1" giving error that it is not declared, how do I declare it in Form2? Make a class? Once I create seperate class how to define "Shape" in it?

For Each shp As RectangleShape In ShapeContainer1.Shapes
  DirectCast(shp, RectangleShape).Dispose()
Next

or

For Each shp As Shape In ShapeContainer1.Shapes
  If shp Is RectangleShape Then
    ShapeContainer1.Shapes.Remove(shp)
  End If
Next`

Sorry guys I am a newbie, could be very easy answer but I am not able to figure out nor able to find the answer

Code from comment:

Dim shapesList1 As New List(Of PowerPacks.Shape)
For Each shp As PowerPacks.Shape In ShapeContainer1.Shapes
  shapesList1.Add(shp)
Next

Dim shapesList2 As New List(Of PowerPacks.Shape)
For index As Integer = 0 To shapesList1.Count - 1
  If Not (TypeOf shapesList1.Item(index) Is PowerPacks.OvalShape) Then
    shapesList2.Add(shapesList1.Item(index))
  End If
Next

ShapeContainer1.Shapes.Clear()
ShapeContainer1.Shapes.AddRange(shapesList2.ToArray)
0

There are 0 answers