Click here to Skip to main content
Licence CPOL
First Posted 10 Apr 2005
Views 48,531
Bookmarked 25 times

Fade-in forms using .NET and C#

By | 10 Apr 2005 | Article
Describes how to build forms that fade-in when they load.

Introduction

Did you ever wanted to make your applications more “cool”? Do you like the fade-in effect that Windows XP has? Do you want to include that in your application? If the answers to these questions are all true, then you have come to the right place.

Recently, I wanted to add fading capabilities to my application and searched the net for information on how to do that. Well, the only information I could find was written in VB.NET and my project(s) was written in C#. So, I decided to write some code that will allow my entire application to become “fade-in” enabled.

The idea was to write the code only once, and then re-use it across my entire application(s).

The solution

The solution apparently was very simple: The Windows Form provided by the .NET Framework, has an attribute that is called Opacity. Surprisingly, this attribute controls how opaque the form is (1 for fully opaque and 0 for fully transparent).

So, I thought that I would vary this attribute from 0 to 1 when the form is displayed and it will fade-in my window.

First attempt

The first attempt was to insert my code in the InitializeComponent() method and to use the Thread object to pause between the changing value, in order to create the fade effect.

The code that does that looks like this:

this.Opacity = 0;
this.Show();
for (float f=0.0F;f<1F;f=f+0.05F)
{
    this.Opacity = f;
    Thread.Sleep(20);
}
this.Opacity = 1;

This code actually works, but there is a tiny little problem with it. While the thread waits for 20ms, it is stalled, and nothing else can be rendered on the form. So, the controls on the form will not render until you exit the loop. This will reveal black spots instead of your controls on the form, and this was unacceptable. But, you could try this form if you don’t have any controls on the form you want to fade-in.

The second attempt

This is actually a successful attempt, because I got everything to work the way I wanted it to. The idea in this attempt was the following: Do not block the current thread in order to fade-in, but to use a timer that will trigger every 20ms. On each trigger of the timer, I would increment the opacity of the form by a small amount, and when I reach the value 1, I would disable the timer.

The code that executes each time the timer fires, looks like this:

this.Opacity += 0.05;
if (this.Opacity >=.95)
{
    this.Opacity = 1;
    tmrFade.Enabled = false;
}

The timer is fired in the form constructor along with the calling Show() method of the form, like this:

this.Opacity = 0;
this.Show();
tmrFade.Enabled = true;

As a side effect I can tell you that you don’t have to call the Show() method of the form when you create an instance of it. It suffices to create an instance, and the form will fade-in.

So, the problem was almost solved. I had a way of “pausing” the fade, and by doing it this way, I would not prevent other controls to be rendered.

All I had to do now was to find a way so that I would not have to add this code and the time to each of my forms.

The second attempt completed

As I was saying, by using a timer and adding a few lines of code to the constructor, I could get a form to fade-in nicely. In order to get all my forms to fade-in nicely, I used inheritance.

What is that? Inheritance means that, if you have a class A with two methods and you inherit a class B from class A, you will be able to access the methods of class A within class B. If class A is inherited from class C, then from within class B, I can access the methods of both class A and class C.

Well, this is what I did in my application also: I created a form called BaseForm that derives from System.Windows.Forms.Form. All the forms should derive from this class. In the BaseForm, I added a timer, and coded it as I showed you before.

So I got BaseForm to fade-in. All I had to do to get the other forms to fade was to derive from BaseForm.

This thing worked great, and I got all my forms to fade-in. However, when I added a control to a form, the fading stopped working. The solution to this is to manually enable the timer in each constructor of the derived form as follows:

tmrFade.Enabled= true;

As I was able to figure out, it seems that Visual Studio .NET 2003, when generating code for the designer, does not work great with derived members. I might be wrong though.

Conclusion

As a conclusion, I would like to ask you for your feedback about this article.

Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Alexandru Ghiondea

Software Developer
Microsoft
United States United States

Member

I am working on the C# compiler at Microsoft since 2007.
 
Microsoft Certified Professional since 2006
 
Interests: C#, ASP.NET, LINQ

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRe: PinmemberBrian Ferguson18:48 29 Mar '10  
GeneralCalling this.Refresh() PinmemberMathieu Jacques10:16 14 Sep '06  
GeneralCross thread UI update PinmemberS. Senthil Kumar7:55 24 May '05  
GeneralRe: Cross thread UI update PinmemberJudah Himango15:59 21 Jan '06  
GeneralRe: Cross thread UI update PinmemberS. Senthil Kumar5:20 22 Jan '06  
GeneralSeems slow to load. PinmemberBBiillZZ6:19 24 May '05  
GeneralAnimateWindow API Pinmemberdannyres15:11 10 Apr '05  
GeneralRe: AnimateWindow API PinmemberAlexandru Ghiondea22:15 10 Apr '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120515.1 | Last Updated 10 Apr 2005
Article Copyright 2005 by Alexandru Ghiondea
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid