How to Fade a Form using Windows Forms 2.0
Posted by: Suprotim Agarwal ,
on 6/27/2008,
in
Category WinForms & WinRT
Abstract: There have been plenty of articles written on fading out a form. However I found them to contain a lot of code to do this simple thing. In this article, I will demonstrate how to fade out a form with just 5 lines of code.
How to Fade a Form Using Windows Forms
In this short article, I will demonstrate how to use the “Opacity” property of the Form to fade it out.
Let us get started :
Step 1: Open VS2005 and create a new application using either C# or VB.NET. Drag a button on the form. Rename the button to “Fade Me” and set its Name property to “btnFade”.
Step 2: Add the System.Threading directive to your class. We will be making the current thread to sleep for 100 milliseconds so that you can see the effect.
using System.Threading; - C#
Imports System.Threading – VB.NET
Step 3: Double click the button. Write the following code on the btnFade click event
C#
private void btnFade_Click(object sender, EventArgs e)
{
int loopctr = 0;
for (loopctr = 100; loopctr >= 5; loopctr -= 10)
{
this.Opacity = loopctr/95.0;
this.Refresh();
Thread.Sleep(100);
}
this.Close();
}
VB.NET
Private Sub btnFade_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim loopctr As Integer = 0
For loopctr = 100 To 5 Step -10
Me.Opacity = loopctr/95.0
Me.Refresh()
Thread.Sleep(100)
Next loopctr
Me.Close()
End Sub
Check out how we use the form’s opacity to fade it out.
Step 4: Run the application. Click on the button and watch the form fade out.
Cool, ain’t it and with just 5 lines of code. I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn