Windows窗体淡入效果问题VB.Net

时间:2018-12-15 17:38:01

标签: vb.net project fadein

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

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

/home/Desktop

第二个:

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

谢谢大家。

1 个答案:

答案 0 :(得分:0)

这是我想你想要的一个例子... 我试图使它尽可能接近您的原始示例。 为了进行测试,我将代码放入Form_Load中并添加了一个Timer。 希望您可以测试并更改自己的需求。

在设计器中,表单需要将其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范围内。