Sebo
Sebo

Reputation: 441

TransparencyKey Property on Forms

I would like to swith my form background transparency with Visual C# in a Windows Forms Application.

I used

BackColor = Color.White;
TransparencyKey = Color.White;

Now I want to switch back to "not transparent". How can I accomplish that? Just switching the BackColor makes the elements on the form look strange and it feels ugly. I guess there is a way to reset the property.

Upvotes: 0

Views: 4761

Answers (3)

Jim
Jim

Reputation: 1

It's eleven years later, but I ran into this problem. So for any other readers: I set the transparency key mistakenly. A post from another forum said you could right click it, at design, and then reset it, but reset was disabled (grayed out). So I simply backspaced out the 'White' (in design) that was set. (I am confident it set it to 'Color.Empty' as one of the guys here noted.) Problem solved. I should say that I was using an ancient version of .net. Hopefully this will also work in more up to date versions.

Upvotes: 0

Isti115
Isti115

Reputation: 2748

this is the original value:

this.TransparencyKey = Color.Empty;

You could set this, and then nothing will be transparent.

Upvotes: 5

Chris Shain
Chris Shain

Reputation: 51339

How about storing the previous values of BackColor and TransparencyKey in local variables, and restoring them when you want to revert to non-transparent? For instance:

private Color _oldBG;
private Color _oldTPKey;

private void MakeTransparent() {
    _oldBG = BackColor;
    _oldTPKey = TransparencyKey;
    BackColor = Color.White;
    TransparencyKey = Color.White;
}

private void MakeNonTransparent() {
    BackColor = _oldBG;
    TransparencyKey = _oldTPKey;
}

Upvotes: 1

Related Questions