0

I'm using 3 pictureboxes. 1 as background & 2 as transparent layer over the background. all with same size. Layer 1 is used to draw lines & layer 2 to draw shapes. I'm using tab control to control which layer is visible and which is hidden. but somehow cant make both layer visible at the same time eventhough they both transparent.

The code I'm using

public Form1()
        {
            InitializeComponent();
            bgLayer.Image = bmp;
            bgLayer.Controls.Add(lineLayer);
            bgLayer.Controls.Add(squareLayer);
            lineLayer.Location = new Point(0, 0);
            squareLayer.Location = new Point(0, 0);
            lineLayer.BackColor = Color.Transparent;
            squareLayer.BackColor = Color.Transparent;
        }

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                lineLayer.Visible = true;
                squareLayer.Visible = true;
                lineLayer.Enabled = false;
                squareLayer.Enabled = false;
            }
            else if (tabControl1.SelectedIndex == 1)
            {
                lineLayer.Visible = true;
                squareLayer.Visible = false;
                lineLayer.Enabled = true;
                squareLayer.Enabled = false;
            }
            else if (tabControl1.SelectedIndex == 2)
            {
                lineLayer.Visible = false;
                squareLayer.Visible = true;
                lineLayer.Enabled = false;
                squareLayer.Enabled = true;
            }
        }

Anyone know how to make both transparent layer visible at the same time? tab control 0 is both visible, 1 is picturebox1 only & 2 is picturebox3 only. Tab control 1 & 2 works fine but 0 only shows layer picturebox1.

tried adding lineLayer.Controls.Add(squareLayer); but it makes the program buffers non stop when executed

  • I think picturebox1 overwrite picturebox3 because both locations have the same. – Vishal Parmar Jan 08 '20 at 08:42
  • They __need__ to be __nested__ one by one. - So change `pictureBox2.Controls.Add(pictureBox3);` maybe to `pictureBox1.Controls.Add(pictureBox3);` . But do pick a plausible order and naming scheme! – TaW Jan 08 '20 at 09:45
  • @TaW tried that before and the program wont stop buffering when executed. and sorry for the file names will edit it soon – Novan Noviandri Sumarna Jan 08 '20 at 23:50
  • Get rid of your control layers. Just do all your painting in one PictureBox control. – LarsTech Jan 09 '20 at 00:03
  • _the program wont stop buffering_ I have no idea what that means but I know that it will work if done right..: `pbox3.Parent = pbox2; pbox2.Parent=pbox1; pbox1.Parent = pBoxBase;` – TaW Jan 09 '20 at 02:07

1 Answers1

-2

This is not possible using WinForm's PictureBox: WinForms does not support z-ordering of alpha-blended (or even index-transparency) controls the way you can using WPF or HTML+CSS. The only thing it does allow is for controls to re-render their parent Control's background before they paint themselves (note that parent controls also necessarily clip their children too, as all Control subclasses in WinForms encapsulate a User32 hWnd. The only workaround is to create a new top-level window without a non-client area, which can be painful).

The only workaround is to have a single control that's custom painted to redraw the stacked images inside its overridden OnPaint event or to regenerate an in-memory Bitmap every time you want the appearance to change and use a single PictureBox See here: Make overlapping picturebox transparent in C#.net

Dai
  • 141,631
  • 28
  • 261
  • 374
  • _The only thing it does allow is for controls to re-render the parent Form's background before they paint themselves._ Wrong. Correct: _The only thing it does allow is for controls to re-render the respective parents' background before they paint themselves._ – TaW Jan 09 '20 at 02:08
  • @TaW thank you for the correction - I’ve updated my answer. – Dai Jan 09 '20 at 02:59