954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Simple way to add a fade effect

By Quinton Zinn on Jan 13th, 2011 4:05 am

Using the Load and Unload events of the Forms add these subs to handle a simple fade in and out effect. This is not limited to just one form. The subroutine definition can be added to any global class allowing the use on any form or sub-form.

'Handle Fade in of form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
fade_in()
End Sub

'Handle Fade Out of form
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
fade_out()
End Sub

'Fade in
Public Sub fade_in()
For FadeIn = 0.0 To 1.1 Step 0.1
Me.Opacity = FadeIn
Me.Refresh()
Threading.Thread.Sleep(100)
Next
End Sub


'Fade out:
Public Sub fade_out()
For FadeOut = 90 To 10 Step -10
Me.Opacity = FadeOut / 100
Me.Refresh()
Threading.Thread.Sleep(50)
Next
End Sub

Plain and simple, nice.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

This one is nice and simple...!!!!

Thanks man....!!!

nytro
Newbie Poster
9 posts since Jan 2011
Reputation Points: 10
Solved Threads: 2
 

Thanx to Unhnd_Exception For a Mod.. Here is a sub that will work for all controls or forms when the form shows. There is a revision issue for the form based on your .NET version and/or the VB Studio version as the libraries handle the code diffrently. Kudos for the Mod!!

Private Sub FadeForm(ByVal TotalSeconds As Single)
    If TotalSeconds = 0 Then
        Me.Opacity = 1
        Exit Sub
    End If

    Dim [then] As Double = DateAndTime.Timer
    Dim difference As Double = 0

    'difference is the percentage of the total seconds elapsed
    Do While difference < 1
        Me.Opacity = difference
          
        difference = (DateAndTime.Timer - [then]) / TotalSeconds
        System.Threading.Thread.Sleep(10)
    Loop

    Me.Opacity = 1

End Sub

Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    FadeForm(1.5)
End Sub
zinnqu
Junior Poster in Training
86 posts since Jun 2009
Reputation Points: 21
Solved Threads: 15
 

C# code please..........

stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 

Code has been posted in the C# snippet forum today.

zinnqu
Junior Poster in Training
86 posts since Jun 2009
Reputation Points: 21
Solved Threads: 15
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: