vb.net配置picturebox并重新加载它们

时间:2013-11-06 01:28:32

标签: vb.net winforms controls dispose

嘿,我的胜利表格中有大约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分钟,因此有足够的时间来抓取图像并保存。

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

任何帮助都会很棒..我确信它只是一些我想念的简单......

1 个答案:

答案 0 :(得分: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
相关问题