24 April, 2012

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.

Step 1: Open Visual Studio 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.
Isn't it a new way to give an animation close effect form to your user.
Try it out !!


5 comments:

  1. My brother recommended I may like this website. He
    was entirely right. This submit actually made my day.
    You cann't imagine just how much time I had spent for this info!
    Thank you!

    My site: wordpress

    ReplyDelete
  2. Very shortly this web site will be famous amid all blogging
    visitors, due to it's good posts

    Look at my web page; wordpress developer

    ReplyDelete
  3. Tremendous things here. I'm very glad to look your post. Thank you so much and I
    am taking a look ahead to touch you. Will you kindly drop me
    a e-mail?

    My site :: wordpress plugin developer

    ReplyDelete
  4. Thanks for ones marvelous posting! I truly enjoyed reading it, you can be a great author.I will always bookmark your blog and will eventually come back later on.
    I want to encourage that you continue your great work, have a nice weekend!



    Take a look at my site: social network links

    ReplyDelete
  5. Everyone loves what you guys tend to be up too. This type of clever work and exposure!
    Keep up the wonderful works guys I've included
    you guys to my own blogroll.

    Look at my web blog; baju kantor pria

    ReplyDelete