用代码加载图片

时间:2016-12-10 10:23:21

标签: vb.net

Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each pictire As PictureBox In Me.Controls
        If Not pictire.Tag.Equals("n") Then
            pictire.Image = Image.FromFile("peg.png")
        End If
    Next
End Sub

这是我的代码不起作用。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您循环遍历Me(可能是表单)中包含的所有控件 在该集合中,不仅仅是PictureBoxes,因此您需要过滤以仅获取那些:
(请参阅OfType

Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each pictire As PictureBox In Me.Controls.OfType(Of PictureBox)
        If Not pictire.Tag.Equals("n") Then
            pictire.Image = Image.FromFile("peg.png")
        End If
    Next
End Sub