-1

The code below allows me to fade in and out when it opens and closes, which is what I want. However, I would like my form to remain open for 10 seconds before the fading starts. I am struggling to get that part done.

Here is what I have so far:

Public Class frmDefinitions

    Private Sub Button1_Click(sender As Object, e As EventArgs) _
                    Handles Button1.Click
        tmr_out.Enabled = True
    End Sub

    Private Sub frmDefinitions_Load(sender As Object, e As EventArgs) _
                    Handles MyBase.Load
        Me.Opacity = 100
        tmr_in.Enabled = True
    End Sub

    Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                    Handles tmr_in.Tick
        Me.Opacity += 0.05
        If Me.Opacity = 1 Then
            tmr_in.Enabled = False
        End If
    End Sub

    Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                    Handles tmr_out.Tick
        Me.Opacity -= 0.05
        If Me.Opacity = 0 Then
            tmr_out.Enabled = False
            Me.Close()
        End If
    End Sub

End Class
4

1 回答 1

2

您需要设置第三个Timer来延迟 tmr_out 的启动Timer。一旦您的 tmr_in 被禁用,我就会触发延迟。然后,您应该在开始淡出之前获得 10 秒的延迟。您也可以尝试使用表单的Shown事件来启动延迟,但您需要调整 10 秒以适应淡入延迟。

Public Class Form1
    Dim tmrDelay As New Timer()

    Public Sub New()
        InitializeComponent()
        tmrDelay.Interval = 10000
        AddHandler tmrDelay.Tick, AddressOf tmrDelay_Tick
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.Opacity = 0
        tmr_in.Enabled = True
    End Sub

    Private Sub tmrDelay_Tick(sender As System.Object, e As System.EventArgs)
        tmrDelay.Stop()
        tmr_out.Start()
    End Sub

    Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_in.Tick
        Me.Opacity += 0.05
        If Me.Opacity = 1 Then
            tmr_in.Enabled = False
            tmrDelay.Start()  'Start Your 10 second delay here.
        End If
    End Sub

    Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_out.Tick
        Me.Opacity -= 0.05
        If Me.Opacity = 0 Then
            tmr_out.Enabled = False
            Me.Close()
        End If
    End Sub


End Class
于 2013-08-04T22:47:27.427 回答