Jaan Suur
Jaan Suur

Reputation: 41

How can i apply invisible background to a control on panel? (C#)

TransparencyKey is not working when applied to a Control which is on a panel, the panel's invisible background is working.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.TransparencyKey = Color.FromArgb(0, 0, 1);
        panel1.BackColor = Color.FromArgb(0, 0, 1);
        button1.BackColor = Color.FromArgb(0, 0, 1);
    }
}

button1 is the Control on panel1. button1 still has its original backcolor (30,30,30)

Upvotes: 0

Views: 68

Answers (1)

Kallum Tanton
Kallum Tanton

Reputation: 777

According to the documentation for Color.FromArgb you are currently calling the method using the "RGB" overload - the values you're specifying are only populating the "RGB" part of the colour and ignoring the "A" or "alpha" part. You need to use the overload that accepts four arguments:

button1.BackColor = Color.FromArgb(0, 0, 0, 1);

Notice the 0 at the begining - this is the alpha property, setting it to 0 makes the colour transparent. The clue is in the method name - "ARGB" - which denotes the order in which to specify the arguments.

From MS Docs:

FromArgb(Int32, Int32, Int32, Int32)

Creates a Color structure from the four ARGB component (alpha, red, green, and blue) values. Although this method allows a 32-bit value to be passed for each component, the value of each component is limited to 8 bits.

Upvotes: 1

Related Questions