Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#
Article

Fade-in forms using .NET and C#

Rate me:
Please Sign up or sign in to vote.
3.09/5 (20 votes)
10 Apr 2005CPOL4 min read 87.7K   4.5K   26   8
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:

C#
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:

C#
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:

C#
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:

C#
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)


Written By
Software Developer Microsoft
United States United States
I am working on the C# compiler at Microsoft since 2007.

Microsoft Certified Professional since 2006

Interests: C#, ASP.NET, LINQ

Comments and Discussions

 
GeneralRe: Pin
AbleOus29-Mar-10 18:48
AbleOus29-Mar-10 18:48 
GeneralCalling this.Refresh() Pin
Mathieu Jacques14-Sep-06 10:16
Mathieu Jacques14-Sep-06 10:16 
GeneralCross thread UI update Pin
S. Senthil Kumar24-May-05 7:55
S. Senthil Kumar24-May-05 7:55 
GeneralRe: Cross thread UI update Pin
Judah Gabriel Himango21-Jan-06 15:59
sponsorJudah Gabriel Himango21-Jan-06 15:59 
GeneralRe: Cross thread UI update Pin
S. Senthil Kumar22-Jan-06 5:20
S. Senthil Kumar22-Jan-06 5:20 
GeneralSeems slow to load. Pin
BBiillZZ24-May-05 6:19
BBiillZZ24-May-05 6:19 
GeneralAnimateWindow API Pin
kaptaintens10-Apr-05 15:11
kaptaintens10-Apr-05 15:11 
GeneralRe: AnimateWindow API Pin
Alexandru Ghiondea10-Apr-05 22:15
Alexandru Ghiondea10-Apr-05 22:15 

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

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