-1

这是我在 vb.net 中使用计时器自动更改图像的代码,但代码不起作用......

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Me.BackgroundImage = Image.FromFile("C:\images\\" + Label1.Text + "1.jpg")
    Dim i As Integer = Convert.ToInt32(Label1.Text)
    i += 1

    If (i > 4) Then
        i = 1
    End If
    Label1.Text = i.ToString()
 End Sub
4

1 回答 1

0

将计数器移到班级级别。也包含来自 dbasnett 的评论。另外,标签不应该反映当前的图像编号吗?你拥有它的方式标签中的值是背景图像的一个“超前”。

尝试更多类似的东西:

Private i As Integer = 1

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim FileName As String = "C:\images\" & i.ToString & ".jpg"
    Try
        Me.BackgroundImage = Image.FromFile(FileName)
        Label1.Text = i.ToString()

        i += 1
        If (i > 4) Then
            i = 1
        End If
    Catch ex As Exception
        Debug.Print(FileName)
        MessageBox.Show(FileName & vbCrLf & vbCrLf & ex.ToString, "Error Loading Image")
    End Try
End Sub
于 2013-10-03T20:38:21.543 回答