CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Aug 2008
    Posts
    111

    Question Vb.Net Dispose Picturebox(Es) And Load Them Again

    Hey all i have about 30 pictureboxes on my win form. It populates images every time a new song gets played (think of it as "previous played list").

    The pictureboxes look like so on the form (3 row of 10)
    Code:
         ___  ___  ___  ___  ___  ___  ___  ___  ___  ___
         | |  | |  | |  | |  | |  | |  | |  | |  | |  | |
         ---  ---  ---  ---  ---  ---  ---  ---  ---  ---
    
         ___  ___  ___  ___  ___  ___  ___  ___  ___  ___
         | |  | |  | |  | |  | |  | |  | |  | |  | |  | |
         ---  ---  ---  ---  ---  ---  ---  ---  ---  ---
    
         ___  ___  ___  ___  ___  ___  ___  ___  ___  ___
         | |  | |  | |  | |  | |  | |  | |  | |  | |  | |
         ---  ---  ---  ---  ---  ---  ---  ---  ---  ---
    This is how i am going about populating the pictureboxes on the form:
    Code:
        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
    I have tried:
    Code:
        If InStr(ctlControl.name, "previous_") <> 0 Then
             ctlControl.Image = Nothing
             ctlControl.Controls.Clear()
             ctlControl.dispose()
        End If
    This does not seem to dispose the images as it loops... it doesn't even seem to find the pictureboxes on the form? When i stop my form and then start it again it populates the image just fine. Keep in mind that each image is grab as the song plays so its not due to it not having the image ready to place in the list - its an average of 3 minutes per song so thats plenty of time to grab the image and save it.

    It seems to not be finding the picturebox controls that i programmable coded onto the form...

    Any help would be great.. I'm sure its just something simple that i am missing...

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Vb.Net Dispose Picturebox(Es) And Load Them Again

    Two things..

    1.) I would create the Picturbox control on form load and simply switch out the images without recreating and disposing of them everytime..

    2.) Using image buffering will also assist in this process..

    read this Image animation article for info and code samples on how to use image buffering..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured