我有带按钮的 Form1,它创建了一些矩形,另一个按钮在同一个 Form1 上删除了这些矩形(这是通过下面的代码工作的)
canvas.Parent = Nothing
现在在 Form1 中,我有另一个按钮,它打开带有一组值、复选框和文本框的 New Form2
这个 Form2 有一个复选框,一旦选中 OK,它就会在“FORM1”上生成更多的矩形
现在我需要 Form2 上的复选框/按钮来重置值和矩形。所以基本上当这个复选框被选中时,它应该只删除通过这个表单制作的矩形,而不是 Form1 中背景中的所有矩形。
前任。我在 Form1 中已经有 5 个矩形,当我打开 Form2 并输入几个值时,它又创建了 2 个矩形,所以现在我在 Form1 中有 7 个矩形。假设我喜欢矩形的大小或方向,所以我在 Form2 中选中了 1 个框,它删除了最后 2 个矩形,所以最后在 Form1 中剩下 5 个矩形。希望我足够清楚!
我尝试了很多选项,但没有一个有效(例如:我想删除名为“Block”的 RectangleShape 或只是 RectangleRhape
Block.Dispose()
Block.Invalidate()
Form1.Controls.Find("Block", True)
Block.Dispose()
DirectCast(Controls(dummy), RectangleShape).Dispose()
然后我发现下面的这些代码有“Shapecontainer1”的问题,给出了没有声明的错误,我如何在Form2中声明它?开课?一旦我创建了单独的类,如何在其中定义“形状”?
For Each shp As RectangleShape In ShapeContainer1.Shapes
DirectCast(shp, RectangleShape).Dispose()
Next
或者
For Each shp As Shape In ShapeContainer1.Shapes
If shp Is RectangleShape Then
ShapeContainer1.Shapes.Remove(shp)
End If
Next`
对不起,我是新手,可能很容易回答,但我无法弄清楚也无法找到答案
评论中的代码:
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)