שתף באמצעות


Displaying a Label that shows a PictureBox behind it

Question

Saturday, December 16, 2006 2:13 AM

I have a VB 6 app I'm converting to VB 2005. It displays a series of JPG files in a PictureBox, and three labels.  In the old application, the letters on the labels float over the image background because the labels' BackStyle was Transparent.  But in the new version the labels (with a transparent BackColor) appear in a rectangle whose color is the BackColor of the underlying form. What's the secret to making the picture-box rather than the form appear behind the label text?

All replies (5)

Saturday, December 16, 2006 4:23 PM ✅Answered

A transparent control shows the colour of its parent. So

Label1.Parent = PictureBox1

will allow it to show the picturebox picture.

You will probably have to reposition it after changing its parent.

 


Saturday, December 16, 2006 3:15 PM

Don't use a picture box, use a panel.  Then set the background image to whatever picture you want.  Does a similar thing and the labeling isnt' a problem. 


Saturday, December 16, 2006 6:41 PM

> A transparent control shows the colour of its parent

That did the trick for displaying the background image. 

> You will probably have to reposition it after changing its parent

True; the labels now sit inside the image even when the image doesn't fill the screen. I want to have them occupy a fixed point on the display; can a child control appear outside the rectangle of its parent control?  Worst case, I could calculate whether the image is above a certain height and vary the Parent property of the label to the Picbox or the form, depending.

Thanks to both of you; I'll try the Panel alternative later and compare the results.


Saturday, December 16, 2006 7:29 PM

I don't think you can make a child control appear outside the bounds of its parent.

If you have SizeMode set to Normal what about making the picture box the same size as the form and make all the controls children of the picturebox?


Saturday, December 16, 2006 9:46 PM

> I don't think you can make a child control appear outside the bounds of its parent

I think you're right.

> If you have SizeMode set to Normal what about making the picture box the same size as the form and make all the controls children of the picturebox?

Alas, I don't have that option; in order to resize the images to fit as close as possible to full-screen without distortion, I fiddle with the picbox SizeMode at run time between AutoSize and Zoom. But it wasn't too difficult to add the test:

If picBox1.Left < 4 And picBox1.Top < 8 Then

lblFName.Parent = picBox1 'label within picbox if picbox ~fullscreen

Else

lblFName.Parent = Me ' otherwise, label outside picbox

End If

And since the picbox is centered in the form, the same test works for labels at all corners. (It's always nice to find a legal shortcut.)