Search code examples
c#formstransparency

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.


Solution

  • 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;
    }