0

I would like to make a label's background color transparent as it's put over two picture boxes, one picture box is used for the background of the form and the other is simply a picture on the form.

This is how the winform looks without running it: Before run

The label "Game ID:" also has a transparent background, but over the background picture. I used the following code to do so:

 label1.Parent = pictureBox1;
 label1.BackColor = Color.Transparent;

No problem here. However when I try the same technique with the numbers on the board the labels disappear (I call the labels hole_1, hole_2....ect):

hole_1.Parent = pictureBox2;
hole_1.BackColor = Color.Transparent;

This is how the window looks like after implementing the two codes above: After run

As you can see the label vanishes.

Maya
  • 47
  • 7
  • 1
    `Label` is a control so it has enabled state, visibility, can be clicked, can be focused, can capture keystrokes, can indicate focused state with a dotted rectangle, etc. Are you sure you want to use a control here with all its overheads and bulkiness? If not, then I would suggest to use the `Paint` event of the `PictureBox` instances to draw the additional texts, eg. `e.Graphics.DrawString(...)` or `TextRenderer.DrawText(e.Graphics, ...)` in the event handler. Whenever you need to replace the text, just call `Invalidate()`, which triggers a new repaint session. – György Kőszeg Dec 03 '21 at 17:29
  • Btw, the `Label` also has an `Image` property. To display text over the image just set both the `ImageAlign` and `TextAlign` properties to `ContentAlignment.MiddleCenter` so they are both centered. – György Kőszeg Dec 03 '21 at 17:36

1 Answers1

1

The location property is relative to the upper left of the controls container. In this case, the location was originally set with the FORM as the parent. When you switch to the picturebox as the parent but keep the same location, the control will be shifted down and to the right, possibly putting it off the visible portion of the control.

One solution is to convert the location to screen coords, and then back to client coords w/ respect to the picturebox:

Point pt = this.PointToScreen(hole_1.Location);
hole_1.Parent = pictureBox2;
hole_1.Location = pictureBox2.PointToClient(pt);
hole_1.BackColor = Color.Transparent;
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40