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.

In this VB.NET tip, I will show you a way to give users a form that fades out when it's closed. This isn't a must-have functionality for applications, but I think 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:

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:

        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, I set the Form's Opacity to 0.99 (the difference is not visible to the user) and then fade the form out.

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Related links

Comments

1

Mirko Vucicevic - 20/12/07

This works fine, except one thing. After I fade out an open dialog the main form blinks - it refreshes, and I don't know how to avoid this.

» Report offensive content

2

Jonas Wranå - 20/04/08

Works very good. Even in fullscreen it fades without blinks. I programmed my own first with bitlocking and lowlever graphics, but this was better!

Thanks.

» Report offensive content

3

Dominique - 17/06/08

Nice code. Just my two cents (something I did wrong myself): do not add this code to your form_closing event. That's because, at the end you have me.close, which calls the form_closing event... Solution? If you put it in form_closing, just remove me.close.

In short, sweet code, thanks!

Dominique

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

3

Dominique - 17/06/08

Nice code. Just my two cents (something I did wrong myself): do not add this code to your form_closing event. That's ... more

2

Jonas Wranå - 20/04/08

Works very good. Even in fullscreen it fades without blinks. I programmed my own first with bitlocking and lowlever graphics, but ... more

1

Mirko Vucicevic - 20/12/07

This works fine, except one thing. After I fade out an open dialog the main form blinks - it refreshes, and ... more

Log in


Sign up | Forgot your password?

  • Staff Apple to developer: Fart jokes aren't funny

    When Apple announced it would be vetting every application submitted for inclusion in the App Store, this was just the kind of question that entered many a mind: just how arbitrary would the company be in wielding that veto power? Read more »

    -- posted by Staff

  • Staff Chrome is just another browser

    Hands up if you missed the Chrome release -- didn't think anyone did. Google's browser arrived with all the fanfare and hype that only Google can produce. Read more »

    -- posted by Staff

  • Renai LeMay 2Vouch refers well

    Melbourne-based Web start-up 2Vouch yesterday launched the first public beta of what it dubs its "social recruiting platform". Read more »

    -- posted by Renai LeMay

What's on?