Transparent forms (form opacity)

Transparent forms (form opacity)
Covers how to change the opacity of a form (making a form transparent) and making only portions of the form transparent using TransparencyKey.

Making a form transparent is farely simple, you only need to set one property.

Add to buttons to a form (btnIncrease and btnDecrease), one will increase the opacity of the form and one will decrease it, thus making it more transparent.

Also if you want to check out the current opacity you can add a TextBox (txtCurrOp) that will display it.

Now double click each of the buttons. For the Click event of the btnDecrease button use the following code:

// Decrease the opacity of the form by 0.01

this.Opacity -= 0.01;

// Update the TextBox

txtCurrOp.Text = this.Opacity.ToString();

And for the btnIncrease Click event:

// Increase the opacity of the form by 0.01

this.Opacity += 0.01;

// Update the TextBox

txtCurrOp.Text = this.Opacity.ToString();

Here’s how the form looks with the opacity 0.75 (that means 75%):

Making portions of the form transparent (TransparencyKey)

But there’s more than this, you can set the opacity for different portions of the form using the TransparencyKey property. By setting this property to a specified color any part of the form that has that color will be transparent.

Let’s test this, double click the form to get to the Form1_Load event. Paste the following code there:

this.TransparencyKey = Color.Black;

This will make everything that’s black on the form, transparent. The text on the buttons, label and TextBox is black, so are the Minimize, Maximize and Close buttons, thus they will be transparent. Here is the form on top of a photo (clouds):

But there’s another way of using TransparencyKey. Add a Panel (Panel1) to the form with the BackColor property set to Red.

Inside the Load event of Form1 use the following:

this.TransparencyKey = Color.Red;

As you might expect, Panel1, being red will actually be transparent:

Also by using TransparencyKey you can create custom shaped forms, but this will be covered in a soon to come tutorial.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top