Fading Effect In VB.Net

Discussion in 'Visual Basic [VB]' started by pradeep, Nov 30, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    While core functionality and useability of the application is always one of the most important aspects, various visual effects may improve the way users feel about a particular application. Even though you may be able to implement purely cosmetic features in an application, it's important to keep the functionality in mind.

    Here we will see how to give users a form that fades out when it's closed. This isn't a must-have functionality for applications, but this feature is often nice to have so that an application closing doesn't look abrupt.

    Form Fade Out



    In order to make the form fade out, I use the Form's Opacity property. By default, the Form's Opacity equals 1. To let the form fade out, I create a look to gradually decrease the Form's Opacity value until I finally close the form.

    Example



    To try this out, add a button to the form and add the following code to its click event:

    Code:
     Private Sub FadingForm()
     
              Dim iCount As Integer  
     
              For iCount = 90 To 10 Step -10
                  Me.Opacity = iCount / 100
                  Me.Refresh()
                  Threading.Thread.Sleep(50)
              Next
     
              Me.Close()
     
     End Sub
    Then add the following code to the Form_Load event:
    Code:
     Me.Opacity = 0.99
     

    How It Works



    On the Form_Load event, I set the Form's Opacity to 0.99 (or 99%). This is because, on some computers, the code provided would initially create a blink and then let the form fade out. This usually happens when the Form's Opacity is lowered from 1 down. To prevent this blink before the fade out effect, we set the Form's Opacity to 0.99 (the difference is not visible to the user) and then fade the form out.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice