2

I have a button that dynamically adds up to 24 labels to the form. The problem is their location.

I figured out that instead of manually setting a specific location for each, I can have them automatically arranged like a FlowLayoutPanel does. But the FLP will be on top and hide controls under it. And sending it to back is worse. So i want to bring it to front but keep it transparent, so that it doesn't hide the other controls under it.

Any suggestions will be great

Thanks.

TGamer
  • 529
  • 1
  • 9
  • 26
  • A `FlowLayoutPanel` is not magic. When you use one, you don't have to specify the `Location` of each child control because it contains the code to calculate them itself. If you're using a `Panel` then there is no such code contained within it so you have to calculate them yourself. There's no way around that. You could inherit `Panel` and encapsulate that code, but you still have to write the code, one way or another. – jmcilhinney Jan 05 '17 at 04:10
  • @ jmcilhinney - Ok I will edit my post and make things more clear. (Way more clear...) – TGamer Jan 05 '17 at 12:04
  • If necessary I will post a screenshot of my design – TGamer Jan 05 '17 at 12:18
  • So, are you saying that you want the `Labels` to appear in front of the image? Have you considered using a `FlowLayoutPanel` and setting its `BackgroundImage`? I've never tried it myself with that control but I would think that it should work. – jmcilhinney Jan 05 '17 at 12:36
  • No that doesn't, because each picturebox can be panned inside its pannel. I can't make it a simple background image. Also, the the `pic`'s can be zoomed in or out. It looks like I have to either make them all at one place or code their locations one by one. – TGamer Jan 05 '17 at 21:27

1 Answers1

3

I can have them automatically arranged like a FlowLayoutPanel does. But I can't use one because it will be on top and hide the images in my pic's.

Well, you can use a FlowLayoutPanel with a transparent background so it doesn't hide the other controls you have. How to do that? Well, this answer shows you how to make a transparent Panel. You should be able to easily adjust it to work with a FlowLayoutPanel using something like this:

Public Class TransparentFLP
    Inherits FlowLayoutPanel

    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H20 ''#WS_EX_TRANSPARENT
            Return cp
        End Get
    End Property
    Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
        ''#MyBase.OnPaintBackground(e) --> Don't uncomment this orelse
                                          'it will cause the BackColor to be redrawn.
    End Sub

End Class
  • Add a new Class to your project.
  • Paste the above code into it.
  • Rebuild your project.
  • Drop the new control from the top of the toolbox onto your form (instead of using the original FlowLayoutPanel).
  • You're good to go.

P.S. I'm not sure about the use of your 4 panels, but you might consider using a TableLayoutPanel instead.

Hope that helps :)

Community
  • 1
  • 1