0

I'm creating a simple form with WinForms with dotNET7 using VS2022:

enter image description here

The gray rectangle is a Panel, which I need to make transparent. You can see a label underneath the panel, and I wish it to be visible, while the panel itself is brought to front.

With dotNET framework, it was so easy:

public class TransparentPanel : Panel
{
  protected override CreateParams CreateParams
  {
    get
    {
      var cp = base.CreateParams;
      cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
      return cp;
    }
  }

  protected override void OnPaintBackground(PaintEventArgs e)
  {
    //base.OnPaintBackground(e);
  }
}

However, with dotNET7 and VS2022 (don't really know which one causes the limitation), when I change the Panel to "TransparentPanel", designer simply gets rid of it:

With original panel, these are my components:

enter image description here

After changing panel1 from System.Windows.Forms.Panel to TransparentPanel:

enter image description here

I tried changing panel's background to transparent, but it's not the giving me my required results:

this.panel1.BackColor = System.Drawing.Color.Transparent;

enter image description here

Also tried to apply color with alpha:

this.panel1.BackColor = Color.FromArgb(100, Color.Red);

enter image description here

So how can I achieve this very simple thing with dotNET7?

Daniel
  • 2,318
  • 2
  • 22
  • 53
  • https://stackoverflow.com/a/36102074/14171304 – dr.null Jan 21 '23 at 17:43
  • 1
    Works fine when I try it with .net6, as expected. The "gets rid of it" problem is what you should pursue, there is no other good way to do this. Normally that only happens when adding the control causes an exception at design-time. That should produce a message box with the exception details, be sure to state what it says if you see it. Another way to debug that is to start VS again and attach to DesignToolsServer.exe, force it to stop on any CLR exception. – Hans Passant Jan 21 '23 at 18:07
  • Remove the `OnPaintBackground` override; in the Control's Constructor, add `SetStyle(ControlStyles.Opaque, true)`, as shown here: [Translucent circular Control with text](https://stackoverflow.com/a/51435842/7444103) – Jimi Jan 22 '23 at 11:33
  • I'm getting CS1540 compile error: "Cannot access protected member 'Control.SetStyle(ControlStyles, bool)' via a qualifier of type 'Panel'; the qualifier must be of type 'Form1' (or derived from it)" – Daniel Jan 22 '23 at 20:50
  • 1
    That method is called from the Constructor of your `TransparentPanel` Custom Control. Just take a look at the Custom Control in the Q&A I've linked – Jimi Jan 23 '23 at 06:06

0 Answers0