C# Tips

C# Tip Article

How to fade in (or fade out) my form

Question

How can I fade in and/or fade out my form (WinForms)?

Tip

You can fade in your form when the form is loaded (at Form_Load event handler). 

And similarly you can fade out when the form is closing (at Form_FormClosing event handler).
In order to fade in/out, you can gradually increment or decrement Opacity property of the form object.

Answer

To fade in the form, you can increment the form's Opacity gradually at Form_Load().

// Fade in
private void Form_Load(object sender, EventArgs e)
{
	this.Opacity = 0;
	while (this.Opacity < 1)
	{
		this.Opacity += 0.01;
		Thread.Sleep(5); // adjust this value for speed
	}
}

To fade out the form, you can decrement the form's Opacity gradually at Form_FormClosing().

// Fade out
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
	this.Opacity = 1;
	while (this.Opacity > 0)
	{
		this.Opacity -= 0.01;
		Thread.Sleep(5);
	}
}