0

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)
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Mak
  • 3
  • 6
  • You can't remove items from a collection while you are iterating that collection, so you can't do a for-each. Try going backwards: `For i as Integer = ShapContainer1.Shapes.Count - 1 To 0 Step -1` – LarsTech Aug 26 '15 at 23:18
  • Thanks @LarsTech , I tried that but it is giving error saying "Shape is not member of windowsapplication1.shapecontainer1" could you help me define shapecontainer1 or shapecontainer1.shape? – Mak Aug 26 '15 at 23:37
  • Isn't it Shapes? I don't use the PowerPack, so I can only go by what your code showed me. – LarsTech Aug 26 '15 at 23:40
  • Oh sorry, I meant "Shapes" only not Shape. It is Powerpack RectangleShape I am trying to dispose. Since I am abel to draw on Form1 from Form2 control, I should be able to dispose it too right? But its posing lot of problem – Mak Aug 26 '15 at 23:45
  • This is what I found online which matches with what you said before but same shapecontainer1 error. `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)` – Mak Aug 26 '15 at 23:47
  • Not sure why line break dint work, sorry it looks mixed up! the above example is for ovalshape which is same. – Mak Aug 26 '15 at 23:53
  • Can someone please tell me why my `ShapeContainer1.Shapes` is giving error? How can I define it in Form2 so that it cater for Form1 RectangleShape – Mak Aug 27 '15 at 05:25
  • You may want to study [this](http://stackoverflow.com/questions/30840241/how-to-connect-tab-controls/30861756?s=1|0.0000#30861756) and [this post](http://stackoverflow.com/questions/22728201/two-picturebox-in-two-different-forms/22729125?s=6|0.0000#22729125)! – TaW Aug 30 '15 at 10:48
  • @TaW Thanks for the post. I am a newbie and trying to understand what you try to explain in those posts. I tried creating RectangleShapes on Form1 from Form2 and it created, working Fine. The problem comes when I am trying to delete that same RectangleShape from Form2. It is giving Error. What I have gathered so far is posted above. I have no clue currently how to go around it. I followed what you said as well " 3 general purpose method" but its not working. Could you please suggest if my approach is right? Could you possibly explain why it is giving error at ShapeContainer1.Shapes ? – Mak Aug 31 '15 at 02:09
  • still no answer to this question! – Mak Aug 24 '16 at 01:51

0 Answers0