Click here to Skip to main content
Click here to Skip to main content
Go to top

How to create a fading form.

, 22 Jan 2004
Rate this:
Please Sign up or sign in to vote.
This article will walk you through the steps required to create a form that fades in upon activation and fades out upon deactivation.

Fader Form

Introduction

This article will show you how to create a form that will fade out when deactivated and fade in when activated.

Background

I was looking for a way to make the active form in an application stick out from the other non-active forms in a visually appealing way. I really liked the fade in/out effect used with the Start Menu of XP and 2000, but I was unable to find any help on forms that had that effect. So, I set out to create a form that would have the fade effect and allow for the user to easily distinguish the active form from the others. The result is the FaderForm and it provides a nice visual effect and makes the active form readily identifiable to the user.

Using the code

Basically, we are just going to start with a basic Windows Form, add a timer to increment/decrement the percent of Opacity, handle two Form events (Activated & Deactivate) and add some initialization code to the constructor of the FaderForm class.

First, start off with a plain form. Set the following properties to these values:

  • Text: FaderForm
  • Name: FaderForm

Second, add a Timer control to the form and set the following properties to these values:

  • Name: FadeTimer
  • Interval: 1
  • Enabled: False

You now have all of the components required. Now, in order to have the form fade in and out we have to handle two events, Activated and Deactivate. The actual incrementing/decrementing of the Opacity of the form is done in the FadeTimer_Tick event handler of the FadeTimer component. The Activated and Deactivated event handlers will set the Status flag and enable the FaderTimer. When the Status flag is set to FADEIN, the FadeTimer_Tick event handler will increase the Opacity, when Status is set to FADEOUT, the FadeTimer_Tick event handler will decrease the Opacity. The FadeTimer_Tick event handler increments the Opacity to 100% or decrements to 50% (depending upon the Status flag) then shuts itself off. One tick is handled every ffTransitionTime milliseconds. Let's start with the class members first.

First, we will need to create four private class members at the beginning of the FaderForm class. The members are as follows:

  • FadeFlag: Type Enum. An enumeration that provides the values FADEIN and FADEOUT used in the Status variable.
  • Status: Type FadeFlag. Used to store the status of the desired transition effect.
  • intTransitionStep: Type Integer. The amount to increment or decrement the Opacity.
  • bFadeEnabled: Type Boolean. Variable used to enable or disable the fade effect.
Public Class FaderForm
Inherits System.Windows.Forms.Form
    
'Enumeration for the current status of the window.
Private Enum FadeFlag
    FADEIN
    FADEOUT
End Enum

'Var to keep track of which transition is 
'going to take place (fade in or out)
Private Status As New FadeFlag()

'Var to hold the increment amount when 
'increasing/decreasing the Opacity
Private intTransitionStep As New Integer()

'Var used to enable or disable the fade in/out effect
Private bFadeEnabled As New Boolean()

Next, add three public properties (to keep the properties grouped together in the Properties List, they all have the prefix ff for faderform). The range checking can be changed to suit your needs.

  • ffTransitionTime: Amount of time in milliseconds that elapses before incrementing or decrementing the Opacity by intTransitionStep.
  • ffTransitionStep: The amount that the Opacity is incremented or decremented.
  • ffFadeEnabled: The variable used to enable or disable the fading effect.

The property ffTransitionTime is defined as:

'TransitionTime: The amount of time (in milliseconds) that elapses before
'increasing or decreasing the Opacity by intTransitionStep.
Public Property ffTransitionTime() As Integer
    Get
        Return FadeTimer.Interval
    End Get
    Set(ByVal Value As Integer)
    'If the value is between 1 ms and 1000ms it's valid
    'If not, set the intTransitionTime to 1 (default value)
    If Value <= 0 Or Value > 1000 Then
        FadeTimer.Interval = 1
    Else
        FadeTimer.Interval = Value
    End If

    End Set
End Property

The property ffTransitionStep is defined as:

'TransitionStep:  The value used to increment the Opacity.
Public Property ffTransitionStep() As Integer
    Get
        Return intTransitionStep
    End Get
    Set(ByVal Value As Integer)

        'If the incremented value is between 1 and 50 it's valid
        'If not, set it to the default value of 2
        If Value < 1 Or Value > 50 Then
            intTransitionStep = 5
        Else
            intTransitionStep = Value
        End If

    End Set
End Property

The property ffFadeEnabled is defined as:

Public Property ffFadeEnabled() As Boolean
    Get
        Return bFadeEnabled
    End Get
    Set(ByVal Value As Boolean)

        bFadeEnabled = Value

        If Value = False Then
            'Since the fade in/out effect is being turned off, set 
            'the opacity to 100%, disable the timer, and even though 
            'it's not necessary set the Status to FADEIN.
             Me.Opacity = 1.0
             FadeTimer.Enabled = False
             Status = FadeFlag.FADEIN

        End If

    End Set
End Property

Finally, write the event handlers for the following:

  • FadeOut : Handles the Deactivate event of the form.
  • FadeIn: Handles the Activated event of the form.
  • FadeTimer_Tick: Handles the Tick event of FadeTimer.

The event handler for FadeOut:

'FadeOut: Fired when the form loses focus through code or by the user. 
'If the bFadeIn_Out value is set to true then 
'this routine will set the Status
'flag to FADEOUT, the timer will gradually decrease the opacity of the form.
Private Sub FadeOut(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Deactivate

    'First check the status of the FadeIn_Out property.
    If bFadeEnabled = True Then

        'Set the status to FadeOut
        Status = FadeFlag.FADEOUT

        'Turn the timer ON
        FadeTimer.Enabled = True

    End If

End Sub

The event handler for FadeIn:

'FadeIn:Fired when the form is activated through code or by the user. 
' If the bFadeIn_Out value is set to true then this 
'routine will set the Status
' flag to FADEIN, the timer will gradually increase the opacity of the form.
Private Sub FadeIn(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Activated

    'First check the status of the FadeIn_Out property.
    If bFadeEnabled = True Then

        'Set the status fo FadeIn
        Status = FadeFlag.FADEIN

        'Turn the timer On
        FadeTimer.Enabled = True

    End If

End Sub

The event handler for FadeTimer_Tick:

'FadeTimer_Tick:  When the timer is enabled, 
'it will check the value held in the 
' Status variable.  If the value is FADEIN the form's opacity is
' gradually increased or decreased by the value held in intTransitionStep
' every ffTransitionTime milliseconds.
Private Sub FadeTimer_Tick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles FadeTimer.Tick
    
    'Check the value of Status
    If Status = FadeFlag.FADEIN Then

        'We know the status is set to "fade in". Increase the Opacity
        'Check the level of opacity
         If Me.Opacity >= 1 Then
             'Shut the timer off.  Our work is done here.
             FadeTimer.Enabled = False
         Else
             'Increase the opacity by 1%
             Me.Opacity = Me.Opacity + intTransitionStep / 100.0
         End If

    Else

         'We know the status is set to "fade out". Decrease the Opacity
         'Check the level of opacity
         If Me.Opacity <= 0.5 Then
             'Shut off the timer. Our work is done here
             FadeTimer.Enabled = False
         Else
             'Decrease the opacity
             Me.Opacity = Me.Opacity - intTransitionStep / 100.0
         End If

    End If

End Sub

Finally, we need to add some initialization code in the constructor of the FaderForm class.

Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    'Set the status of the form when it is created.

    'Set the default values for the FaderForm.
    Status = FadeFlag.FADEIN
    FadeTimer.Enabled = False
    intTransitionStep = 5
    bFadeEnabled = True

End Sub

Your FaderForm is now ready to go. In the example I provided the FaderForm is built into a class library within the folder MMM, the project that implements it is contained within the folder AnotherTest. Simply click on the AnotherTest.sln file, build the example application and have fun! Suggestions and comments are always appreciated, thanks for reading my article!

History

  • May 12, 2003 - Uploaded original article and code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

mightymiracleman
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Generalfade in effect Pinmemberhabib_fci20-Oct-08 2:04 
Questionnot working on child form Pinmemberjathoo27-Aug-07 8:03 
QuestionSplash no form PinmemberFlash042921-Mar-07 11:14 
QuestionPanel Focus ? Pinmembertimelessdw22-Jun-05 22:22 
GeneralSplash Screen PinsussNyein Chan10-Jan-05 5:35 
GeneralRe: Splash Screen PinsussAnonymous5-Feb-05 11:39 
GeneralProgmatically Deactivating a Form PinmemberReisses6-Oct-04 7:06 
GeneralRe: Progmatically Deactivating a Form Pinmembermightymiracleman22-Oct-04 13:52 
QuestionHow to Capture a Blended Form Pinmemberpathmaker21-Sep-04 16:42 
AnswerRe: How to Capture a Blended Form Pinmembermightymiracleman22-Oct-04 13:48 
GeneralSeems like a lot of work when... PinsussAnonymous18-Sep-04 6:34 
GeneralRe: Seems like a lot of work when... PinsussAnonymous18-Sep-04 16:10 
GeneralRe: Seems like a lot of work when... PinmemberIamLamb19-Oct-04 21:38 
GeneralWarning about resize performance Pinmemberstevehiner27-Jan-04 13:13 
GeneralRe: Warning about resize performance PinmemberDr Nick27-Sep-06 6:14 
GeneralRe: Warning about resize performance Pinmemberstevehiner29-Sep-06 6:10 
QuestionWhat about... PinmemberCristoff23-Jan-04 2:12 
AnswerRe: What about... Pinmembermightymiracleman23-Jan-04 3:34 
GeneralGood Code. PinmemberMartin C Cook14-May-03 2:22 
GeneralMDI Child Form Pinmembermikasa13-May-03 10:35 
GeneralRe: MDI Child Form Pinmembermightymiracleman14-May-03 2:53 
GeneralRe: MDI Child Form Pinmembermikasa14-May-03 3:31 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 23 Jan 2004
Article Copyright 2003 by mightymiracleman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid