2017-08-12 1 views
-1

Ich habe 3 Knöpfe: 1ºbtn addiert 3 pictureboxes in flowlayoutpanel. 2ºbtn versuchen, die Kontrollen von Flow zu entfernen 3ºbtn wieder nur 2 PictureBox in flowlayoupanelWiederverwendungskontrollen bei flowlayoutpanel

die i (Loop) verwenden, um Code hinzufügen PictureBox als PictureBox1 genannt hinzuzufügen, ist picturebox2 in Flow dies:

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 

die entfernen Codes habe ich versucht, diese sind:

FlowLayoutPanel1.Controls.clear() 

ich habe auch versucht:

While 
(FlowLayoutPanel1.Controls.Count >)FlowLayoutPanel1.Controls.RemoveAt(0) 
End While 

auch:

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 

Ich habe auch versucht:

For i As Integer = 1 to 3 
If Me.Controls.ContainsKey("PictureBox" & i) Then 
Me.Controls("PictureBox" & i).Dispose() 

Alle, die Codes funktioniert gut zu entfernen, aber dann füge ich kann nicht wieder die gleichen PictureBox in jede Flow wieder ...

Antwort

1

Wenn Sie ein Steuerelement aus einem Container entfernen, müssen Sie es zu einem anderen hinzufügen, bevor Sie den Verweis verlieren. Denken Sie daran, dass jedes Objekt, das nicht von irgendwas referenziert wird, letztendlich vom GC erfasst (gelöscht) wird. Wenn Sie also Remove() oder Clear() aufrufen, fügen Sie sie sofort zum Hauptformular oder einer lokalen Sammlung auf Formularebene hinzu, damit sie im Speicher verbleibt.

+0

Ja! das hat mein Problem gelöst !! nach 3 Tagen!! Ich füge auf die gleiche Weise die Steuerelemente zu form1 hinzu! Ich habe das ... ich bin nur supossing nur die Formen funktioniert als Container ... sehr einfach! Danke vielmals! – arc95

0

Wie von dotNET angegeben, müssen Sie einen Verweis auf das Objekt pflegen.

Beispiel: -

//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) ... 
Verwandte Themen