单个按钮,图片框中的多个图像

时间:2014-12-05 15:29:42

标签: vb.net visual-studio-2013

此代码使用2个按钮更改图像。 如何只使用一个按钮更改图片框中的图像? 是否有任何代码可以确定用户已经点击按钮的次数?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    PictureBox1.Image = Image.FromFile("C:\1.jpg")
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    PictureBox1.Image = Image.FromFile("C:\2.jpg")
End Sub

1 个答案:

答案 0 :(得分:0)

关于“确定用户已点击按钮的次数”的问题有点不清楚,但我认为您指的是哪个图片正在显示

您可以使用if语句执行此操作。

要查看点击次数,您可以使用昏暗的“Dim CurPicture As Integer = 1 “
在你的if语句中,你检查CurPicture的数量“所以如果1显示图片1”
因此,每次单击按钮时,都会向CurPicture

添加1

如果要显示最后一张图片,如果要循环回图片1,则需要制作CurPicture 1

Dim CurPicture As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If CurPicture = 1 Then
        PictureBox1.Image = Image.FromFile("C:\1.jpg")
        CurPicture = 2

    ElseIf CurPicture = 2 Then
        PictureBox1.Image = Image.FromFile("C:\2.jpg")
        CurPicture = 3

    ElseIf CurPicture = 3 Then
        PictureBox1.Image = Image.FromFile("C:\3.jpg")
        CurPicture = 4

    ElseIf CurPicture = 4 Then
        PictureBox1.Image = Image.FromFile("C:\4.jpg")
        CurPicture = 5

    ElseIf CurPicture = 5 Then
        PictureBox1.Image = Image.FromFile("C:\5.jpg")
        CurPicture = 1
    End If

End Sub