Click here to Skip to main content
12,622,747 members (27,708 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.2K views
700 downloads
20 bookmarked
Posted

Simple Form Fading

, 2 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Enable forms to be faded in and out

Introduction

This article will show you how you can add fading to your forms. The code can be used at any time regardless of what the current opacity of the form is.

Using the code 

The code can be implemented by either adding Fade.cs to your project, or by adding a reference to Fade.dll.

To use the code, add a using statement to include the Fader namespace.

using Fader; 

 Calling the method is then a simple task. The syntax for the fading methods is:

Fade.direction(form);    

The direction is either In or Out, and the form is the one to fade. We will now look at the In and Out methods in more detail. 

While both methods can be called in the same way, by just supplying the target form, each is overloaded allowing their behavior to be modified. The In method can be called by passing one property as such:

Fade.In(this); // Fade the current form in, starting at its current opacity

or it can be called with its overloaded method which is called with an additional boolean value. 

Fade.In(this, true); // Fade the current form in, starting at an opacity of 0

The boolean value in this overload is FromHidden which sets the opacity of the form to 0 before fading in. This allows the method to be used in the Form_Load event without having to manually set the opacity to 0.

As with the In method, the Out method can also be called using either one or two properties. Calling the method using only the Form property is done in the same way as the In method: 

Fade.Out(this); // Fade the current form out, starting at its current opacity

The overloaded method accepts the Exit boolean value which closes the application once the fade out has finished. 

Fade.Out(this, true); // Fade the current form out then exit the application 

Development 

I originally attempted to carry out the fading process within a loop using System.Threading.Thread.Sleep(). This worked perfectly for fading forms out, however it did not work for fading the form in. I then turned to using the Windows Forms timers. The timers used are created programmatically within the In and Out methods and disposed once the fading is complete. 

The Fade class three private global variables which are used to pass the properties from the public In and Out methods to the private FadeIn and FadeOut methods which are used as the event handlers for the timers. 

Using timers worked well, however there was an issue with the fading in. Due to Windows adding its own effect to forms when they are shown, the fading is somewhat overshadowed. To overcome this, there is a 1 second delay before the fade in begins to allow the built in effect to pass. The form's opacity is set to 0 before the delay to hide it during the built in effect. This is the code for the In method accepting only the form 

public static void In(Form Form)
{
    frmTarget = Form; // Set global variable
    Form.Opacity = Form.Opacity - (Form.Opacity % 0.05); // Make opacity a multiple of 0.05
    Timer newTimer = new Timer(); // Create new timer for fade in event
    newTimer.Interval = 1; // Set timer interval
    if (blnFromHidden) { newTimer.Interval = 1000; } // Delay for calling from Form_Load
    newTimer.Tick += new EventHandler(FadeIn); // Add event handler to tick event
    newTimer.Start(); // Start fade in event
}

The fading works by adding 0.05 to the opacity on the Tick event of the timer. Because of this, the opacity needs to be a multiple of 0.05. This is achieved by subtracting the remainder of the opacity divided by 0.05. The timer is then created with an interval of 1 millisecond. The code then checks to see if blnFromHidden is set to true. This is set in the overloaded method which also sets the form's opacity to 0. 

public static void In(Form Form, bool FromHidden) // Overloaded method
{
    if (FromHidden)
    {
        Form.Opacity = 0; // Ensure form is hidden before beginning fade in
        blnFromHidden = true;
    }
    In(Form);
}

If blnFromHidden is true, the timer interval is set to 1000. This is where the 1 second delay comes from. Once the timer interval is set, the Tick event is set to the FadeIn method and the timer is started. 

private static void FadeIn(object sender, EventArgs e)
{
    (sender as Timer).Interval = 1; // Remove delay if applicable
    if (frmTarget.Opacity != 1)
    {
        frmTarget.Opacity += 0.05; // Increase form opacity if it is not yet 100%
    }
    else
    {
        (sender as Timer).Stop(); // Stop timer when form opacity is 100%
        (sender as Timer).Dispose(); // Free up resources
    }
} 

The FadeIn method first sets the interval to 1. This ensures that the second long delay is only applied once. If the opacity isn't 1, it is increased by 0.05, if it is 1, the timer is stopped and, disposed.

The Out method works in a similar way to the In method but without the delay. 

public static void Out(Form Form)
{
    frmTarget = Form; // Set global variable
    Form.Opacity = Form.Opacity - (Form.Opacity % 0.05); // Make opacity a multiple of 0.05
    Timer newTimer = new Timer(); // Create new timer for fade out event
    newTimer.Interval = 1; // Set timer interval
    newTimer.Tick += new EventHandler(FadeOut); // Add event handler to tick event
    newTimer.Start(); // Start fade out event
}
public static void Out(Form Form, bool Exit)
{
    blnExit = Exit; // Set global variable
    Out(Form);
} 

When the form has been hidden and the timer has been stopped, if blnExit is true, the application will exit. 

private static void FadeOut(object sender, EventArgs e)
{
    if (frmTarget.Opacity != 0)
    {
        frmTarget.Opacity -= 0.05; // Decrease form opacity if it is not yet 0%
    }
    else
    {
        (sender as Timer).Stop(); // Stop timer when form opacity is 0%
        (sender as Timer).Dispose(); // Free up resources
        if (blnExit)
        {
            Application.Exit(); // Exit application after form is hidden
        }
    }
} 

Plans for improvement 

To further improve this feature, I plan to add the ability to change the speed of the fade to allow the fading to be customised slightly. 

License

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

Share

About the Author

Matt S Smith
Software Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionForm helper Pin
Highflyer2-Apr-13 16:44
memberHighflyer2-Apr-13 16:44 
AnswerRe: Form helper Pin
Lloyd (Chris) Wilson28-Oct-13 12:48
memberLloyd (Chris) Wilson28-Oct-13 12:48 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 2 Apr 2013
Article Copyright 2013 by Matt S Smith
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid