Software Development

See how simple it is to create a fading form in VB.NET

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, Irina Medvinkskaya shows how to give users a form that fades out when it's closed.

While core functionality and usability 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 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.

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.

----------------------------------------------------------------------------------------

Get Visual Basic tips in your inbox

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!

22 comments
bright.amofa
bright.amofa

it worked for me...really like it. but i want more on how to add animations to my forms in visual basic.any help will be appreciated

Arijit.Ravi
Arijit.Ravi

I was expecting something else... Fading form meant (for me) that the form has 100% opacity at the top and gradually it becomes 0% gradient type... if you know how to do THAT, post your awesome code please!

pstransit
pstransit

does anyone have to code for wordwrap and delete for the menustrip vb 2008 a list for all the other codes to. please email me at paul.forster@ntlworld.com ta.

jordan1
jordan1

How can I get this to work in Access 97?

xhorxhius2000
xhorxhius2000

Does anybody know how to implement the same functionality in C#? thanks.

mohamedsaleh70
mohamedsaleh70

Thank you for this simple nice code. It is very applicable.

JRL21
JRL21

Works for me. Thanks for the cosmetics man.

litus82
litus82

I love it. I added this to all my applications. Thanks!

Lionfan1991
Lionfan1991

I've been using a routine like this: Public Shared Sub FadeOut(ByRef TheForm As System.Windows.Forms.Form) Dim SW As New Stopwatch Dim StartValue As Double = TheForm.Opacity SW.Start() While SW.ElapsedMilliseconds < 250 AndAlso TheForm.Opacity > 0 TheForm.Opacity = StartValue - (SW.ElapsedMilliseconds / 250) : Application.DoEvents() End While SW.Stop() End Sub

divineprime
divineprime

This code example has been around for awhile, and I used to use it in my app, until I found that sometimes it becomes super sluggish. It's not a consistent problem, so one would have the false sense of security that the sub always works just fine. Well in fact, it does not. The last thing a user wants to see, is that an app is not closing right away, after they have clicked the close button, or used some other method. The code itself probably would work fine, but when used in real world situations, it will fail for apparently no reason. It's that simple.

divineprime
divineprime

I strongly suggest you dont distribute your applications, for use on other PC's just because it seems to work on yours. Just a tip.

JohnShell
JohnShell

Instead of fading out how would I get a form to fade in?

irina_medvinskaya
irina_medvinskaya

The code provided in the example works as desired to allow you to fade the form. But it's provided only as an example. You are free to modify it as you see fit to use in your applications.

Justin James
Justin James

I have not used this code, but if there is a situation like you describe, it would most likely be related to the Threading.Thread.Sleep(50) statement. Putting a thread to sleep and then waking it does not always happen precisely when we want it to, and 50 milliseconds is enough time for something else to grab the thread scheduler enough so that this is no longer in the fore front of priorities when it wakes. At the very least, I would assign the thread "real time" priority before putting it to sleep. Also, I would most likely use a SpinWait than sleep; it is more CPU intensive than sleep, but will be more accurate for this purpose. On the other hand, my personal preference is to let the OS's current theme dictate things like window fading anyways, and let the OS do it if the user wants. :) J.Ja

divineprime
divineprime

I've tried many variations of the code in my app, and not one of them changed a thing. It still takes way to long to fade out sometimes(not all the time). Even when I commented out the whole class, except for the fade in/fade out.

alaniane
alaniane

that since your only trying to go for visual effects, if it would not be better to just use a timing loop and not bother to use threading. I do agree that it is better to generally let the OS take care it. The simpler I can keep my app the better; especially, when the app runs in a distributed environment.

divineprime
divineprime

Okay I've just stuck the offending code back into my application, and BAM slowwww... 15 seconds later the fade out completes. J.Ja is on the right track is his diagnosis. I've commented out everything except the load and unloading. When I unload I create a new thread to do some simple shut down work, as the form is fading. This really seems to be what is interfering. However in a multithreaded situation(real world) this is bound to happen. If you have nothing going on at the time of the fade, it should work perfect as planned. If there is, or may be a thread doing work, it will cause the fade out to take a long time. The user may close the form at any time though, so this is not as clear cut as one would think. As J.Ja says, let's allow the OS to dictate window themes, and the fading, until there is a good code example that works in all situations.

patricia.lynn
patricia.lynn

Is it possible to do this "fading forms" trick successfully on regular MS Access database forms?

divineprime
divineprime

A thread safe timer component, or other should work.

patricia.lynn
patricia.lynn

I am new to this site, and I can't figure out how to contact you directly, but you said in your post that you have different methods of making the Access database forms fade out. I would like you to either post them or e-mail them to me, if you would. Also, I saw another question, asking if it's possible to make a control fade out. Do you know the answer to this (and the instructions, if it is possible). Remember, I'm not all that computer literate, so the instructions have to be idiot-proof, if you want to give them to me. Thank you very much.

jevans10253
jevans10253

Patricia.lynn, there are ways different then what is posted here. I have used different variations and can post them or email them to you if requested. It is relatively simple process.

Editor's Picks