0

嘿,我的获胜表格上有大约 30 个图片框。每次播放新歌曲时它都会填充图像(将其视为“以前播放的列表”)。

图片框在表格上看起来像这样(3 行,每行 10 个)

 ___  ___  ___  ___  ___  ___  ___  ___  ___  ___
 | |  | |  | |  | |  | |  | |  | |  | |  | |  | |
 ---  ---  ---  ---  ---  ---  ---  ---  ---  ---

 ___  ___  ___  ___  ___  ___  ___  ___  ___  ___
 | |  | |  | |  | |  | |  | |  | |  | |  | |  | |
 ---  ---  ---  ---  ---  ---  ---  ---  ---  ---

 ___  ___  ___  ___  ___  ___  ___  ___  ___  ___
 | |  | |  | |  | |  | |  | |  | |  | |  | |  | |
 ---  ---  ---  ---  ---  ---  ---  ---  ---  ---

这就是我要在表单上填充图片框的方式:

Private Sub getAlbumArt()
    ...lots of code in here

    If lblArtist.Text <> prevArtiest Then
        prevArtiest = lblArtist.Text
        AddHandler clearPrevPlayed.DoWork, AddressOf clearPrevPlayed_DoWork
        AddHandler clearPrevPlayed.RunWorkerCompleted, AddressOf clearPrevPlayed_RunWorkerCompleted

        clearPrevPlayed.RunWorkerAsync()
    End If
End Sub

Private Sub clearPrevPlayed_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    For Each ctlControl In Me.Controls
        If TypeName(ctlControl) = "PictureBox" Then
            If InStr(ctlControl.name, "previous_") <> 0 Then
                ctlControl.Image = Nothing
                ctlControl.Controls.Clear()
                ctlControl.dispose()
            End If

            If InStr(ctlControl.name, "previousSong_") <> 0 Then
                ctlControl.Image = Nothing
                ctlControl.Controls.Clear()
                ctlControl.dispose()
            End If
        End If

        Application.DoEvents()
    Next ctlControl
End Sub

Private Sub clearPrevPlayed_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    Call buildPrePlayed(lblArtist.Text)
End Sub

Private Sub buildPrePlayed(ByVal theArtest As String)
    Dim intX As Integer = 0
    Dim tmpNewImg(30) As PictureBox
    Dim tmpNewImgPlaceholder(30) As PictureBox
    Dim thePicsNames(30) As String
    Dim filePaths As Linq.IOrderedEnumerable(Of IO.FileInfo) = New DirectoryInfo(Application.StartupPath & "\cdcovers").GetFiles().OrderByDescending(Function(f As FileInfo) f.LastWriteTime)

    For Each fi As IO.FileInfo In filePaths
        If InStr(fi.Name, "_small") <> 0 And intX <= 30 Then
            thePicsNames(intX) = (fi.Name)
            intX += 1
        End If
    Next

    intX = 0

    Do Until intX = 30
        Dim newImgPlaceholder As New PictureBox
        Dim newImg As New PictureBox

        If thePicsNames(intX) <> "" Then
            newImgPlaceholder.Visible = True
            newImgPlaceholder.Image = My.Resources.cdPlaceHolder
        Else
            newImgPlaceholder.Image = My.Resources.BLANK
            newImgPlaceholder.Visible = False
        End If

        newImgPlaceholder.Size = New System.Drawing.Size(130, 130)
        newImgPlaceholder.BorderStyle = BorderStyle.None

        If intX = 0 Then
            newImgPlaceholder.Location = New Point(145, 600)
        ElseIf intX >= 1 And intX < 10 Then
            newImgPlaceholder.Location = New Point((tmpNewImgPlaceholder(intX - 1).Left + 160), 600)
        ElseIf intX = 10 Then
            newImgPlaceholder.Location = New Point(145, 760)
        ElseIf intX >= 10 And intX <= 19 Then
            newImgPlaceholder.Location = New Point((tmpNewImgPlaceholder(intX - 1).Left + 160), 760)
        ElseIf intX = 20 Then
            newImgPlaceholder.Location = New Point(145, 920)
        ElseIf intX >= 21 And intX <= 29 Then
            newImgPlaceholder.Location = New Point((tmpNewImgPlaceholder(intX - 1).Left + 160), 920)
        End If

        newImgPlaceholder.BackColor = Color.Transparent
        newImgPlaceholder.Name = "previous_" & intX

        If thePicsNames(intX) <> "" Then
            newImg.Visible = True
            newImg.Image = Image.FromFile(Application.StartupPath & "\cdcovers\" & thePicsNames(intX))
        Else
            newImg.Image = My.Resources.BLANK
            newImg.Visible = False
        End If

        newImg.Size = New System.Drawing.Size(120, 120)
        newImg.BorderStyle = BorderStyle.None

        If intX = 0 Then
            newImg.Location = New Point(150, 605)
        ElseIf intX >= 1 And intX < 10 Then
            newImg.Location = New Point((tmpNewImg(intX - 1).Left + 160), 605)
        ElseIf intX = 10 Then
            newImg.Location = New Point(150, 765)
        ElseIf intX >= 10 And intX <= 19 Then
            newImg.Location = New Point((tmpNewImg(intX - 1).Left + 160), 765)
        ElseIf intX = 20 Then
            newImg.Location = New Point(150, 925)
        ElseIf intX >= 21 And intX <= 29 Then
            newImg.Location = New Point((tmpNewImg(intX - 1).Left + 160), 925)
        End If

        newImg.BringToFront()
        newImg.Name = "previousSong_" & intX

        Me.Controls.Add(newImg)
        Me.Controls.Add(newImgPlaceholder)

        tmpNewImg(intX) = newImg
        tmpNewImgPlaceholder(intX) = newImgPlaceholder

        intX += 1
    Loop

    RemoveHandler clearPrevPlayed.DoWork, AddressOf clearPrevPlayed_DoWork
    RemoveHandler clearPrevPlayed.RunWorkerCompleted, AddressOf clearPrevPlayed_RunWorkerCompleted
End Sub

我努力了:

If InStr(ctlControl.name, "previous_") <> 0 Then
     ctlControl.Image = Nothing
     ctlControl.Controls.Clear()
     ctlControl.dispose()
End If

这似乎没有在循环时处理图像......它甚至似乎没有在表单上找到图片框?当我停止我的表单然后再次启动它时,它会很好地填充图像。请记住,每张图像都是在歌曲播放时抓取的,所以这不是因为它没有准备好将图像放入列表中 - 每首歌曲平均需要 3 分钟,因此有足够的时间来抓取图像并保存它。

似乎没有找到我可编程编码到表单上的图片框控件...

任何帮助都会很棒..我敢肯定它只是我缺少的一些简单的东西...

4

1 回答 1

0

只需在表单级别保留一个 List(Of PictureBox) 并在创建它们时将它们添加到其中。现在您可以遍历该列表以摆脱它们,而无需再次“找到”它们。

所以它可能看起来像这样:

Private Previous As New List(Of PictureBox)

Private Sub getAlbumArt()

    ' ...lots of code in here

    If lblArtist.Text <> prevArtiest Then
        prevArtiest = lblArtist.Text

        For Each pb As PictureBox In Previous
            pb.Dispose()
        Next
        Previous.Clear()
        Application.DoEvents()

        buildPrePlayed(lblArtist.Text)
    End If
End Sub

Private Sub buildPrePlayed(ByVal theArtest As String)

    ' ...lots of code in here

    intX = 0
    Do Until intX = 30
        Dim newImgPlaceholder As New PictureBox
        Dim newImg As New PictureBox

        Previous.Add(newImgPlaceholder)
        Previous.Add(newImg)

        ' ...lots of code in here

    Loop
End Sub
于 2013-11-06T03:05:01.530 回答