Transparent Custom Graphing User Control: Turning Black on Runtime

2

I have a custom graphing usercontrol which has support for transparent backcolor. During design, the usercontrol shows properly. However, on runtime, the backcolor turns black.

Chart on Runtime

I have searched stackoverflow for a solution, but most of the articles refer to WPF or controllers. :(

Things I have tried:

I have tried using a transparentpanel usercontrol to house the graphing usercontrol. This changed nothing. I set the graphing usercontrol to use the transparentpanel as the parent, nothing.

Does anybody have any input on how to prevent this from happening? Thank you.

Update 1: If I just add the graphing usercontrol to the form and run the app, the transparency still works. It seems to be an issue when it starts graphing. I have tried the following solutions.

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams parameters = base.CreateParams;
            parameters.ExStyle |= WS_EX_TRANSPARENT;
            return parameters;
        }
    }

    internal const int WS_EX_TRANSPARENT = 0x00000020;

I also tried adding:

        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
c#
winforms
user-controls
transparency
graphing
asked on Stack Overflow Nov 20, 2018 by frostbyte • edited Nov 20, 2018 by frostbyte

1 Answer

1

I had a similar problem to this. I searched Google and found my answer. I don't know your code for your custom control, but take a look for a line like this in your OnPaint:

e.Graphics.Clear(Color.Transparent);

The function "Clear" used with the "Transparent" color is not going to make your custom control transparent. It essentially paints the color transparent over your entire control. This can lead to some pretty funky results.

answered on Stack Overflow Nov 20, 2018 by Alexandra

User contributions licensed under CC BY-SA 3.0