Hello!

I'm making a fading splash screen for a software but I can't seem to make the splash screen fade in and then fade out. It only fades in, fades out a bit then stop.

Here's my code.

Thanks in advance!

Private Sub frmSplashScreen_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Opacity = 0.0

        SplashScreenTimer.Start()

    End Sub

    Private Sub SplashScreenTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles SplashScreenTimer.Tick

        Dim Appeared As Boolean = False

        If Not Appeared Then

            Opacity += 0.05

            If Opacity >= 1.0 Then

                Appeared = True

                SplashScreenTimer.Interval = 5000

            End If

        End If

        If Appeared Then

            Opacity -= 0.5

        End If

    End Sub

Try this

Private fadeout As Boolean
.
.
.
Private Sub btnFadeOut_Click(sender As System.Object, e As System.EventArgs) Handles btnFadeOut.Click
    fadeout = True
    Timer3.Enabled = True
End Sub

Private Sub btnFadeIn_Click(sender As System.Object, e As System.EventArgs) Handles btnFadeIn.Click
    fadeout = False
    Timer3.Enabled = True
End Sub

Private Sub Timer3_Tick(sender As Object, e As System.EventArgs) Handles Timer3.Tick

    If fadeout Then
        If Me.Opacity <= 0.0 Then
            Timer3.Enabled = False
        Else
            Me.Opacity -= 0.01
        End If
    Else
        If Me.Opacity >= 1.0 Then
            Timer3.Enabled = False
        Else
            Me.Opacity += 0.01
        End If
    End If

End Sub

My timer interval is set to 10.

10 minute put together example with no timer. You can control how long the fading will take place with the AnimationTime variable

Call the form

Public Class Form1

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        My.Forms.Form2.Show()
    End Sub

End Class

Form fading

Public Class Form2

    'Total time in seconds for the fading.
    Private Const AnimationInSeconds As Double = 0.5

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Opacity = 0
    End Sub
  
    Private Sub Form2_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        Dim StartTime As Double = DateAndTime.Timer
        Dim ElaspedTime As Double = 0

        Do While ElaspedTime < AnimationInSeconds

            'if elasped time is .1 seconds then
            'the opacity will be .1 / .5 = 20%
            Me.Opacity = ElaspedTime / AnimationInSeconds
            ElaspedTime = DateAndTime.Timer - StartTime
            Me.Refresh()
        Loop
        Me.Opacity = 100
    End Sub

    Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        Dim StartTime As Double = DateAndTime.Timer
        Dim ElaspedTime As Double = 0

        Do While ElaspedTime < AnimationInSeconds

            'if elasped time is .4 seconds then
            'opacity will be .4 / .5 = .8  
            '1 - .8 = 20% opacity

            Me.Opacity = 1 - (ElaspedTime / AnimationInSeconds)
            ElaspedTime = DateAndTime.Timer - StartTime
            Me.Refresh()
        Loop
        Me.Opacity = 0

    End Sub

End Class

Thanks for the reply guys!

I'll keep you updated for what happens.

Hi,
Create two timers on the splash screen and try this code
Worked for me.

Public NotInheritable Class SplashScreen1

    'TODO: This form can easily be set as the splash screen for the application by going to the "Application" tab
    '  of the Project Designer ("Properties" under the "Project" menu).
    Dim x As String
    Dim m As Double
    Dim p As Boolean
    Dim tick As Integer

    Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Set up the dialog text at runtime according to the application's assembly information.  

        'TODO: Customize the application's assembly information in the "Application" pane of the project 
        '  properties dialog (under the "Project" menu).

        'Application title
        If My.Application.Info.Title <> "" Then
            ApplicationTitle.Text = My.Application.Info.Title
        Else
            'If the application title is missing, use the application name, without the extension
            ApplicationTitle.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
        End If

        'Format the version information using the text set into the Version control at design time as the
        '  formatting string.  This allows for effective localization if desired.
        '  Build and revision information could be included by using the following code and changing the 
        '  Version control's designtime text to "Version {0}.{1:00}.{2}.{3}" or something similar.  See
        '  String.Format() in Help for more information.
        '
        '    Version.Text = System.String.Format(Version.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor, My.Application.Info.Version.Build, My.Application.Info.Version.Revision)

        Version.Text = System.String.Format(Version.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor)

        'Copyright info
        Copyright.Text = My.Application.Info.Copyright

        x = 1
        p = False
        m = x / 100
    End Sub

    Private Sub ApplicationTitle_Click(sender As System.Object, e As System.EventArgs) Handles ApplicationTitle.Click

    End Sub
    Private Sub MainLayoutPanel_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MainLayoutPanel.Paint

    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        If p = True Then
            Me.Opacity = m
            m = m - 0.01

        ElseIf p = False Then
            Me.Opacity = m
            m = m + 0.01
        End If

        If m = 1.0 Then
            Timer1.Stop()
            Timer2.Start()
        End If

        If m = 0 Then
            Timer1.Stop()
            Me.Close()
            Form1.Show()
        End If
    End Sub

    Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick
        tick = tick + 1

        If tick = 5 Then
            p = True
            Timer1.Start()
        End If
    End Sub
End Class

Thank you guys for your help!

I realized that the fade out wasn't working because I initialized
my boolean variable inside Private Sub SplashScreenTimer_Tick.
This means that every time the timer ticks, my boolean variable is
reinitialized to its default value causing it not to change
its value to true, therefore not reaching the fade out
section in my code.

Edited 4 Years Ago by ryklon: n/a

This question has already been answered. Start a new discussion instead.