0

Why is it that when you drop a control, for example a label, onto a PictureBox in the Designer, that the PictureBox is not the Parent of that label ?

I can set the PictureBox as parent in code like this :

label1.Parent = PictureBox1;

And that works without problems, but I cannot do it using the designer. In that case the control underneath the PictureBox becomes the parent of the label.

Why is this and is there a fix ?

EDIT:
I need this because I need labels over the PictureBox and the labels should have BackColor = Color.Transparent
It seems the Transparent only works if the label has the PictureBox as Parent.

GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • That's simply a design decision; probably because it is meant to display images (in 3 levels) so having controls sit on top seemed not useful. You still can drop them on top and add code to nest&move them.. – TaW Sep 26 '18 at 09:22
  • 1
    Is it not because the picture box should not be a parent of any other controls? – Julo Sep 26 '18 at 09:22
  • If the PictureBox is not meant to be parent, then why does it works in code ? That just makes no sense – GuidoG Sep 26 '18 at 09:22
  • why don't you use panel instead of picturebox, if you want it as a parent control? – Ashkan Mobayen Khiabani Sep 26 '18 at 09:24
  • @AshkanMobayenKhiabani Because I need labels over the picturebox with BackColor = Color.Transparent. And the Transparent only seems to work if the Label has the PictureBox as parent – GuidoG Sep 26 '18 at 09:25
  • Maybe you can inherit PictureBox and make LabeledPictureBox with overloaded OnPaint? with Graphics.DrawString – Access Denied Sep 26 '18 at 09:28
  • @AccessDenied The users need to be able to click on the labels and when hoovering over them the BackColor will change briefly. Dont know how to do that with overloaded onPaint – GuidoG Sep 26 '18 at 09:31
  • @AccessDenied Can I inherit PictureBox and make it a valid container ? – GuidoG Sep 26 '18 at 09:32
  • @GuidoG: Interesting 'feature' _(the transparent colour only for parent)_. I would probably derive my class from `Panel` and set background image to panel. I know that custom `OnPaint` can bring a lot of trouble _(I already created a few controls where I needed this feature - different displaying behaviour from standard controls)_. – Julo Sep 26 '18 at 09:34
  • @Julo I tried just that and it worked. If you write this as answer i will accept it – GuidoG Sep 26 '18 at 09:37

1 Answers1

1

The PictureBox was not designed to be parent of other objects. (Even so, it seems like there is a workaround.)

But the recommended way is to use background image on Panel control:

this.panel1.BackgroundImage = myImage;
Julo
  • 1,102
  • 1
  • 11
  • 19
  • I agree. Background Image on Panel control is what I used. – GuidoG Sep 26 '18 at 11:00
  • I was only not sure whether this control has a background image and how does it work. I remember that I needed make an derived control for buttons to use image in the way I wanted to use them _(but it was only to correctly resize the image as far as I remember)_. – Julo Sep 26 '18 at 11:08
  • Well the solution was actually simple but I did not think of it until your comment. A Panel Control can use an image as background and is a valid container. and the transparancy of the labels on it also works – GuidoG Sep 26 '18 at 11:09
  • 1
    `Label` Transparency work just as well on `PictureBox` and most other Controls. Substituting `Panel` is fine, especially if you make it [DoubleBuffered](https://stackoverflow.com/questions/44185298/update-datagridview-very-frequently/44188565#44188565) - Note the Layout options its BackgroundImage has, which slightly differ from PBox's.. – TaW Sep 26 '18 at 11:34
  • @TaW Its actually on the PictureBox that I found out the Transparancy was working. I had no idea it also worked on Panel and that Panel was able to show a BackGroundImage. – GuidoG Sep 26 '18 at 11:50