Windows 表格淡入淡出问题VB.Net

Windows Form fade in effect problem VB.Net

我对 Windows 表单的淡入淡出效果有疑问。 此表单应读取文件的值并将其存储在变量中,然后启动一个计时器,该计时器应将表单的不透明度增加到读取的值,但它没有发生。

我尝试了两种方法将值存储在变量中,第一种:

If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Users\" & My.Settings.username & "\opacity.goodsetting") Then
        Try
            Using read As New StreamReader(Application.StartupPath & "\Users\" & My.Settings.username& "\opacity.goodsetting")
                If read.ReadLine = "50" Then
                    varOpacity = 0.5
                ElseIf read.ReadLine = "60" Then
                        varOpacity = 0.6
                ElseIf read.ReadLine = "70" Then
                        varOpacity = 0.7
                ElseIf read.ReadLine = "80" Then
                        varOpacity = 0.8
                ElseIf read.ReadLine = "90" Then
                        varOpacity = 0.9
                ElseIf read.ReadLine = "100" Then
                        varOpacity = 1
                End If
                read.Close()
                End Using
        Catch ex As Exception
            varOpacity = 1
            Me.Opacity = 1
        End Try
End If
Timer2.Start()

第二个:

Try
    varOpacity = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Users\" & My.Settings.username & "\opacity.goodsetting")
    Me.Opacity = varOpacity / 100
Catch ex As Exception
    varOpacity = 1
    Me.Opacity = 1
End Try

但这两种方法都不起作用。

我也留下效果码:

Me.Opacity = Me.Opacity + 0.1
If Me.Opacity = varOpacity Then
    Timer2.Stop()
End If

感谢大家。

这是我认为您想要的示例... 我试着让它尽可能接近你原来的例子。 出于测试目的,我将代码放在 Form_Load 中并添加了一个计时器。 希望大家可以根据自己的需要进行测试和修改。

表单需要在设计器中将其 Opacity 设置为 0。 代码将从 0 淡入到文件中存储的值,否则默认为 1。

代码有导入System.IO

Private varOpacity As Double

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

    'Set the path just once.
    Dim pathOpacityLimit As String = Path.Combine(Application.StartupPath & "\Users\" & My.Settings.username, "opacity.goodsetting")
    If File.Exists(pathOpacityLimit) Then
        Try
            Using read As New StreamReader(pathOpacityLimit)
                If Double.TryParse(read.ReadLine, varOpacity) Then
                    'Value parsed - changed to 0.0 > 1.0 so inline with Me.Opacity. Start timer.
                    varOpacity /= 100
                    Timer2.Start()
                Else
                    'Incorrect (not parsed) value in file - set default
                    Me.Opacity = 1
                End If
                read.Close()
            End Using
        Catch ex As Exception
            'Exception - set default
            Me.Opacity = 1
        End Try
    Else
        'File doesn't exist - set default
        Me.Opacity = 1
    End If
End Sub

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    Me.Opacity += 0.1
    If Me.Opacity < varOpacity Then Exit Sub
    Me.Opacity = varOpacity
    Timer2.Stop()
End Sub

我希望这足以让您入门。您还可以检查存储的值,看它是否在所需的 0 到 100 范围内。