Introduction
This article describes a simple approach to fading out a form. The approach used permits the user to define the number of incremental steps used to fade out the form; the form is faded by decreasing the forms opacity from 100% down to 0% using the number of steps defined and by an equivalent amount in each step.

Figure 1: Fading Out a Form (Test Application)
Getting Started
The solution contains two Windows Forms (frmMain.vb and frmDirections.vb) and a single class (FadeEffect.vb). The class contains a single public shared method (FadeForm) which accepts a Windows Form and a byte value as arguments. The form argument is the form to fade out of existence and the byte value allows the user to configure the number of incremental decreases in opacity that will be required to fade out the form (0 to 255). In the examples provided, this value is set to 50 but you can set it to any valid number with larger values creating slower fades and smaller values creating faster fades.
All of the code necessary to fade out a form is contained in the FadeForm class. The two forms call the FadeForm function within their overridden dispose functions such that, whenever either form is disposed of, the fade effect is generated.

Figure 2: Solution Explorer with the Project Visible
Code: Fade Effect (FadeEffect.vb)
This class provides all of the functionality required to fade out a Windows Form. The code for this class should be easy enough to follow. The class begins with the standard library imports; System.Windows.Forms was added as the FadeForm method accepts a Windows Form in its argument list.
Imports System.Windows.Forms
Public Class FadeEffect
The FadeForm function accepts a Windows Form and a byte value as its arguments. The Windows Form identifies the form to be faded; the byte value defines the number of steps though the form will be faded out. For example, if 25 is passed to the function, the form will be faded out in 25 equal steps. The amount of opacity decremented in each step is defined by dividing the starting point (100%) by the number of steps.
<summary>
Function used to fade out a form using a user defined number of steps
</summary>
<param name="FadeForm"></param>
<param name="NumberOfSteps"></param>
<remarks></remarks>
Public Shared Sub FadeForm(ByVal FadeForm As Form, _ByVal NumberOfSteps As Byte)
Dim StepVal As Double =Convert.ToDouble(100.0F / NumberOfSteps)
Dim dOpacity As Double = 100.0F
Dim b As Byte = 0
For b = 0 To NumberOfSteps
FadeForm.Opacity = dOpacity / 100
FadeForm.Refresh()
Opacity -= StepVal
Next
End Sub
End Class
Code: Main Form (frmMain.vb)
This form class is used test the Fade Effect; the overridden dispose method has been modified to call the fade effect whenever the form is closed. Aside from that, the form is used to create an instance of another form (frmDirections.vb) which has also been modified to fade out the form upon disposal.
The code contained in the Dispose call is as follows:
Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Call the fade effect on this form
FadeEffect.FadeForm(Me, 50)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Code: Directions Form (frmDirections.vb)
This form class is also used test the Fade Effect; the overridden dispose method has been modified to call the fade effect whenever the form is closed.
Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Call the fade effect on this form
FadeEffect.FadeForm(Me, 50)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Summary.
This article was intended to demonstrate a simple approach to fading out a form using a variable number of decrements to that forms opacity value. It serves not other purpose than to possibly jazz up the UI a little bit.