Search code examples
vb.netcontrolsreusabilityflowlayout

How reuse correctly controls at flowlayoutpanel?


i have 2 textbox:

  1. Number of products for sale, can be 1-35
  2. Number of products to refuel, can be 1-35

35 hidden pictureboxes with the loaded image of the products from .imagelocation- named picturebox1, picturebox2, picturebox3, etc 2 buttons: Sale & Refuel

Sale button: A loop to make visible the number of images of products available to sale (counting it from the sales-number-textbox), then i sort them adding it into a flowlayoutpanel:

For i As Integer = 1 To numberofproductstosale.Text
            If Me.Controls.ContainsKey("PictureBox" & i) Then
                Me.Controls("PictureBox" & i).Visible = True
                Me.Controls("PictureBox" & i).Margin = New Padding(0)
                Dim p1 As PictureBox = CType(Me.Controls("PictureBox" & i), PictureBox)
                FlowLayoutPanel1.Controls.Add(p1)
            End If
        Next

IT WORKS! and show properly each item... BUT, Then i want to reuse the same flowlayoutpanel to show the refuel items, but i dont reach to do it!

once, i've tried using at REFUEL BUTTON:

FlowLayoutPanel1.Controls.clear()

and then adding similar code to add+unhidde the first time... BUT IT DONT WORK, also i've tried a new loop to remove the 35 elements (picbxs) individually.. and adding again with similar first code but not works, individually the individual remove+hidding looks like this:

For i2 As Integer = 1 To 35
    If Me.Controls.ContainsKey("PictureBox" & i2) Then
       Me.Controls("PictureBox" & i2).Visible = False
       Dim p2 As PictureBox = CType(Me.Controls("PictureBox" & i2), PictureBox)
       FlowLayoutPanel1.Controls.Remove(p2)
    End If
 Next

I think that removes and hiddes correctly because each item dissapear from the screen.. Then i try add and unhidde again the pictureboxes, but now determined by the number of the refuel list (textbox) with this code:

 For i3 As Integer = 1 To productstorefuel.Text
            If Me.Controls.ContainsKey("PictureBox" & i3) Then
                Me.Controls("PictureBox" & i3).Visible = True
                Me.Controls("PictureBox" & i3).Margin = New Padding(0)
                Dim p3 As PictureBox = CType(Me.Controls("PictureBox" & i3), PictureBox)
                FlowLayoutPanel1.Controls.Add(p3)
End If
next

My code looks fine, neat and logic! why dont work? i use i, i2, i3 at diferent loops to avoid confussions... better: i also have tried to unhide-add into a second flow layoutpanel, hidding the first panel and bringing to the front the second panel, but not works!.. i only can get back on the screen the first panel with the sorted items.. that part of the code looks like this

FlowLayoutPanel1.visible = false    
FlowLayoutPanel2.visible = true
 For i3 As Integer = 1 To productstorefuel.Text
            If Me.Controls.ContainsKey("PictureBox" & i3) Then
                Me.Controls("PictureBox" & i3).Visible = True
                Me.Controls("PictureBox" & i3).Margin = New Padding(0)
                Dim p3 As PictureBox = CType(Me.Controls("PictureBox" & i3), PictureBox)
                FlowLayoutPanel2.Controls.Add(p3)
End If
next
    FlowLayoutPanel2.BringToFront()

NOTE: i know the first part (sales part) of the code is fine because my buttons are really checkboxes with button aspect, then, i can see the processing speed 'cause when i uncheck the sales button the pictureboxes just hidde/show very fast but when i click between butons (sales-refuel) i can almost detect slow re-loading of the images of pictureboxes. but clickin at refuel (checking or unchecking) nothing happens!! the screen remains empty... i think the problem is how i remove it. Any suggestions?? thanks in advance!


Solution

  • I solved it this way: the controls (inside flowlayoutpanel) must get back into the form1 container instead of clearing or removing them, by adding them back to form1 in the same way they were added to flowpanel. Then they can be reused correctly!