Make a form fade out until it disappears in C#

A form's Opacity property determines how opaque it is. Opacity = 1 means the form is opaque and Opacity = 0 means the form is transparent. Values between 0 and 1 make a translucent form. (Note that at design time the Properties window displays Opacity values between 0% and 100% instead of between 0 and 1.)

When you click this example's Close button, the code enables a Timer control that reduces the form's Opacity until it disappears.

// Make the form disappear.
private void btnClose_Click(object sender, EventArgs e)
{
tmrFade.Enabled = true;
}

// Decrease the form's opacity by 20%.
private void tmrFade_Tick(object sender, EventArgs e)
{
Opacity -= 0.2;
if (Opacity <= 0) Close();
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.