Recipe 4.16. Creating a Fading Form


Problem

You want a form to fade out and disappear.

Solution

Sample code folder: Chapter 04\FadingForm

Use the form's Opacity property to slowly fade it out. Create a new Windows Forms application, and add a Button control named ActClose to the form. Change the button's Text property to Close. Then add the following source code to the form's code template:

 Private Sub ActClose_Click(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles ActClose.Click    ' ----- Fade out the form.    Dim counter As Integer    For counter = 90 To 10 Step -20       Me.Opacity = counter / 100       Me.Refresh()       Threading.Thread.Sleep(50)    Next counter    Me.Close() End Sub 

Run the program, and click on the Close button to see the form fade away.

Discussion

You'll find that on some systems, the form momentarily blinks to black right when it makes the transition from an opacity of 1.0 to any other opacity value. On such systems, setting the Opacity property to a non-1.0 value during the Load event handler still causes a blink, but it does so when the form first opens, not during the cool fadeout.

 Private Sub AboutProgram_Load(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Me.Load    ' ----- Prepare the form for later fade-out.    Me.Opacity = 0.99 End Sub 




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net