2

I want to have an image partially overlaying a winform control (in this case a datagridview) but it seems that's not working as i would. I followed the answer found here and it works fine when the parent is the control (DGV) but not when the parent is the form...
I will like to have something like this: desired

But instead i get something like this :
problematic

Following the answer and setting the parent to DGV works fine.... enter image description here

John
  • 974
  • 8
  • 16
  • 2
    Looks like the background is not transparent. Share some code what you have tried – EpicKip Aug 09 '17 at 08:38
  • please check in your code whether you missed that line: `b.BackColor = Color.Transparent;` from the example of your posted link – Mong Zhu Aug 09 '17 at 08:39
  • I have updated the question to answer the comment from EpicKip & Mong Zhu – John Aug 09 '17 at 09:35
  • Indeed, winforms does not support transparency for overlapping controls. you may workaround by using two pictureBoxes. See [here for an example using a Label](https://stackoverflow.com/questions/35107257/how-to-add-label-transparency-in-picturebox-c/35107623#35107623). The trick is to nest the 2nd one and move it to the top left until it fits perfectly. – TaW Aug 09 '17 at 09:56
  • I just followed the answer i posted originally on my question...it works just fine – John Aug 09 '17 at 10:32

1 Answers1

2

You can use two PictureBoxes and bring one below the DGV and nest the other. Then move the overlay to the right place.. both should be identical otherwise, i.e. have the same Image and the same SizeMode.

Here is a function that'll do it:

void overlayCtls(Control ctlBase, Control ctlOverlay, Control ctlTgt )
{
    ctlOverlay.BackColor = Color.Transparent;
    ctlOverlay.Parent = ctlTgt;
    ctlOverlay.Location = new Point(ctlBase.Left - ctlTgt.Left, ctlBase.Top - ctlTgt.Top);
}

And the result:

enter image description here

Notes:

  • You explictily need to do the nesting as a DGV is not a container, so it will not be enough to move it in place in the designer.
  • You explictily need to set the BackColor to Transparent even if it was set in the designer. Looks like it will be taken from the parent unless set in code.
  • The nested child control will overlay not only the ClientArea of its Parent but also any Border.
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Cool....this is what i wanted...:) Just to get it correct i will create 2 pictureboxes with identical sizes ...the 1 being dummy and the 1 containing the image...the dummy will be parented to the DGV and the one with the image to the dummy? and then run the method...can you give a little more info.. – John Aug 09 '17 at 10:49
  • Yes. But both should have the same image and also the same properties like size and sizemode! – TaW Aug 09 '17 at 11:02
  • So essentially you have 2 pictureboxes...one child of the control in question and one just under the control...the control child is clipped to the boundaries of the control and the 2nd picturebox just "fills" the rest... – John Aug 09 '17 at 11:14
  • Exactly. Do you get it to work? (Of course you don't really need a reusable function if you have only one such overlay..) – TaW Aug 09 '17 at 11:16
  • Well something in the positioning is giving me a bit of headache but i will work it out...thanks...for the clever idea – John Aug 09 '17 at 11:22