How to Fade a Form using Windows Forms 2.0

Posted by: Suprotim Agarwal , on 6/27/2008, in Category WinForms & WinRT
Views: 33082
Abstract: There have been plenty of articles written on fading out a form. However I found them to contain a lot of code to do this simple thing. In this article, I will demonstrate how to fade out a form with just 5 lines of code.
How to Fade a Form Using Windows Forms
 
In this short article, I will demonstrate how to use the “Opacity” property of the Form to fade it out.
Let us get started :
Step 1: Open VS2005 and create a new application using either C# or VB.NET. Drag a button on the form. Rename the button to “Fade Me” and set its Name property to “btnFade”.
Step 2: Add the System.Threading directive to your class. We will be making the current thread to sleep for 100 milliseconds so that you can see the effect.
using System.Threading; - C#
Imports System.Threading – VB.NET
Step 3: Double click the button. Write the following code on the btnFade click event
C#
private void btnFade_Click(object sender, EventArgs e)
{
     int loopctr = 0;
 
     for (loopctr = 100; loopctr >= 5; loopctr -= 10)
     {
          this.Opacity = loopctr/95.0;
          this.Refresh();
          Thread.Sleep(100);
      }
 
      this.Close();
}
 
VB.NET
Private Sub btnFade_Click(ByVal sender As Object, ByVal e As EventArgs)
                  Dim loopctr As Integer = 0
 
                  For loopctr = 100 To 5 Step -10
                        Me.Opacity = loopctr/95.0
                        Me.Refresh()
                        Thread.Sleep(100)
                  Next loopctr
 
                  Me.Close()
End Sub
Check out how we use the form’s opacity to fade it out.
Step 4: Run the application. Click on the button and watch the form fade out.
Cool, ain’t it and with just 5 lines of code. I hope this article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to my RSS Feed. 
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Santosh Kumar on Tuesday, July 1, 2008 5:20 AM
hi..
this is good way to fade the form useing Opacity option in windows application .

http://www.operativesystems.com
Comment posted by Surender Singh on Sunday, July 6, 2008 2:21 PM
It a cool article
Comment posted by Nuruddin Siraj on Monday, July 7, 2008 1:19 AM
Thank for sharing you knowledge, it's very useful.
Comment posted by adwin on Monday, July 7, 2008 4:24 AM
how do u fade a picturebox into another picturebox?
Comment posted by adwin on Monday, July 7, 2008 4:29 AM
how do u fade a picturebox into another picturebox?
Comment posted by Tjaart on Monday, July 7, 2008 12:15 PM
This is a bad way to fade a form. The reason is that when you make your thread sleep your entire application has to wait for it. The best way to fade a form is in fact to use a timer and then change opacity in the Tick event.


Comment posted by Suprotim Agarwal on Wednesday, July 9, 2008 11:06 PM
adwin: I am sorry but please elaborate your requirement further.

Tjaart: Yes timer is an alternate and good way to do the same. Thanks.
Comment posted by Avinash on Thursday, July 10, 2008 12:47 AM
HI this is use full but this i want to do for a sub form but this also fades when i open it for second time ??
Comment posted by Avinash on Thursday, July 10, 2008 7:05 AM
Hi suprotim how to make fade to UserControls?
Comment posted by reivilos on Thursday, July 31, 2008 8:53 AM
Suprotim Agarwal : "Tjaart: Yes timer is an alternate and good way to do the same. Thanks."

It is not an "alternate and good way to do the same". It is just THE way to go.
Tjaart is right, do not use Thread.Sleep.
Comment posted by Suprotim Agarwal on Thursday, July 31, 2008 9:54 AM
reivilos: Thanks.
Comment posted by Vrain on Monday, January 12, 2009 3:54 AM
Hi..
i got error msg for ur code.... like
'Default3' does not contain a definition for 'Opacity'
'Default3' does not contain a definition for 'Refresh'
'Default3' does not contain a definition for 'Close'
Comment posted by Vrain on Monday, January 12, 2009 3:54 AM
Can u plz help for my problem....
Comment posted by Vrain on Monday, January 12, 2009 3:56 AM
Can u plz help for my problem....
Comment posted by Vrain on Monday, January 12, 2009 4:01 AM
Can u plz help for my problem....
Comment posted by Vrain on Monday, January 12, 2009 5:49 AM
Can u plz help for my problem....
Comment posted by Vijayashree on Wednesday, February 10, 2010 5:11 AM
Superb examples
Comment posted by megabyte on Tuesday, June 8, 2010 7:01 PM
This is great...very helpful.  I didn't know it was this easy.

But, I do agree that Thread.Sleep() is not the best way to do it.
Comment posted by megabyte on Tuesday, June 8, 2010 7:03 PM
Vrain, that's spamming.  Don't do it.

Saying "please" is the best way to get a response, I'm glad you said that.  But don't repeat your comment and fill up space just to try to get attention.
Comment posted by Nelson on Saturday, September 4, 2010 12:34 AM
Thanks.
Very compact code than other available on the Internet.

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook