如何编写一个通用代码来在vb.net 2.0中显示多个picturebox?

时间:2015-03-28 09:55:35

标签: vb.net .net-2.0

Imports System.Threading.Thread
Public Class Form1
Dim delay As Integer = 200
Dim i As Integer = 1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    While i <= 5

        PictureBox1.Visible = True
        PictureBox1.Refresh()
        Sleep(delay)
        PictureBox1.Visible = False


        PictureBox2.Visible = True
        PictureBox2.Refresh()
        Sleep(delay)
        PictureBox2.Visible = False


        PictureBox3.Visible = True
        PictureBox3.Refresh()
        Sleep(delay)
        PictureBox3.Visible = False


        PictureBox4.Visible = True
        PictureBox4.Refresh()
        Sleep(delay)
        PictureBox4.Visible = False


        PictureBox5.Visible = True
        PictureBox5.Refresh()
        Sleep(delay)
        PictureBox5.Visible = False

        i = i + 1
        If i = 6 Then
            i = 1
        End If

    End While
End Sub
我写了上面的代码。它正在工作。但以下代码不起作用。我想最小化代码。我想用20个图片框。而不是上面的代码,我想使用小代码,但它将做同样的工作。请帮帮我。

  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    While i <= 5
        Dim pic As PictureBox
        Dim matches() As Control

        matches = Me.Controls.Find("PictureBox" & i.ToString(), True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is Label Then
            pic = DirectCast(matches(0), PictureBox)
            pic.Visible = True
            pic.Refresh()
            Sleep(delay)
            pic.Visible = False
        End If

        i = i + 1
        If i = 6 Then
            i = 1
        End If
    End While
End Sub
  End Class

1 个答案:

答案 0 :(得分:0)

 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    While i <= 5
        Dim pic As PictureBox
        Dim matches() As Control

        matches = Me.Controls.Find("PictureBox" & i.ToString(), True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is PictureBox Then
            pic = DirectCast(matches(0), PictureBox)
            pic.Visible = True
            pic.Refresh()
            Wait(delay)
            pic.Visible = False
        End If

        i = i + 1
        If i = 6 Then
            i = 1
        End If
    End While
End Sub

这段代码是正确的...对不起我第一次写错了代码..现在它是正确的..谢谢你们...

相关问题