在 flowlayoutpanel 重用控件
reuse controls at flowlayoutpanel
我有 3 个按钮:1ºbtn 将 3 个图片框添加到 flowlayoutpanel 中。
2ºbtn 尝试从流程面板中删除控件
3ºbtn 仅将 2 个图片框添加到 flowlayoupanel
我使用(循环)将名为 picturebox1、picturebox2 的图片框添加到流程面板中的代码是这样的:
For i As Integer = 1 To 3
If Me.Controls.ContainsKey("PictureBox" & i) Then
Me.Controls("PictureBox" & i).Visible = True
Me.Controls("PictureBox" & i).Margin = New Padding(0)
Dim px As PictureBox = CType(Me.Controls("PictureBox" & i), PictureBox)
FlowLayoutPanel1.Controls.Add(px)
End If
next
我试过的删除代码是这些:
FlowLayoutPanel1.Controls.clear()
我也试过:
While
(FlowLayoutPanel1.Controls.Count > )FlowLayoutPanel1.Controls.RemoveAt(0)
End While
还有:
For i As Integer = 1 to 3
If Me.Controls.ContainsKey("PictureBox" & i) Then
Me.Controls("PictureBox" & i).remove()
Me.Controls("PictureBox" & i).Visible = False
我也试过:
For i As Integer = 1 to 3
If Me.Controls.ContainsKey("PictureBox" & i) Then
Me.Controls("PictureBox" & i).Dispose()
所有要删除的代码都可以正常工作,但是我无法再次将相同的图片框再次添加到任何流程面板中...
当您从容器中删除控件时,需要在失去其引用之前将其添加回另一个容器。请记住,任何未被任何对象引用的对象最终都会被 GC 收集(删除)。所以当你调用 Remove()
或 Clear()
时,立即将其添加回主窗体或本地窗体级集合,以便它保留在内存中。
如 dotNET 所述,您需要维护对对象的引用。
示例:--
//To store existing picture boxes-
Dim MyObjects as new list(of Picturebox)
Private sub RemoveObJect(Obj as pictureBox)
//remove the object from flowlayoutpanel
FlowLayoutPanel1.Controls.remove(Obj)
//Add the object to a collection - I would wrap the object into a custom
//Class to hold more information about it
MyObjects.add(Obj)
End Sub
Private sub RestoreObJect(Obj as pictureBox)
//reverse the remove procedure
FlowLayoutPanel1.Controls.add(Obj) //or add to a specific index
MyObjects.remove(Obj)
End Sub
Private Sub RemoveObject(Obj as button) ...
我有 3 个按钮:1ºbtn 将 3 个图片框添加到 flowlayoutpanel 中。 2ºbtn 尝试从流程面板中删除控件 3ºbtn 仅将 2 个图片框添加到 flowlayoupanel
我使用(循环)将名为 picturebox1、picturebox2 的图片框添加到流程面板中的代码是这样的:
For i As Integer = 1 To 3
If Me.Controls.ContainsKey("PictureBox" & i) Then
Me.Controls("PictureBox" & i).Visible = True
Me.Controls("PictureBox" & i).Margin = New Padding(0)
Dim px As PictureBox = CType(Me.Controls("PictureBox" & i), PictureBox)
FlowLayoutPanel1.Controls.Add(px)
End If
next
我试过的删除代码是这些:
FlowLayoutPanel1.Controls.clear()
我也试过:
While
(FlowLayoutPanel1.Controls.Count > )FlowLayoutPanel1.Controls.RemoveAt(0)
End While
还有:
For i As Integer = 1 to 3
If Me.Controls.ContainsKey("PictureBox" & i) Then
Me.Controls("PictureBox" & i).remove()
Me.Controls("PictureBox" & i).Visible = False
我也试过:
For i As Integer = 1 to 3
If Me.Controls.ContainsKey("PictureBox" & i) Then
Me.Controls("PictureBox" & i).Dispose()
所有要删除的代码都可以正常工作,但是我无法再次将相同的图片框再次添加到任何流程面板中...
当您从容器中删除控件时,需要在失去其引用之前将其添加回另一个容器。请记住,任何未被任何对象引用的对象最终都会被 GC 收集(删除)。所以当你调用 Remove()
或 Clear()
时,立即将其添加回主窗体或本地窗体级集合,以便它保留在内存中。
如 dotNET 所述,您需要维护对对象的引用。
示例:--
//To store existing picture boxes-
Dim MyObjects as new list(of Picturebox)
Private sub RemoveObJect(Obj as pictureBox)
//remove the object from flowlayoutpanel
FlowLayoutPanel1.Controls.remove(Obj)
//Add the object to a collection - I would wrap the object into a custom
//Class to hold more information about it
MyObjects.add(Obj)
End Sub
Private sub RestoreObJect(Obj as pictureBox)
//reverse the remove procedure
FlowLayoutPanel1.Controls.add(Obj) //or add to a specific index
MyObjects.remove(Obj)
End Sub
Private Sub RemoveObject(Obj as button) ...