4 Replies - 787 Views - Last Post: 07 August 2015 - 02:55 PM Rate Topic: -----

#1 ranilwallace123   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 09-April 15

load picturebox in flowlayoutpanel for each folder in a directory.

Posted 05 August 2015 - 08:31 AM

Hi, what i want to do is load a picturebox into a flowlayoutpanel for each folder in a directory. Then when the user clicks on the picture box, all the pictureboxes will be removed and a custom control i made will be added to the flowpanel for each mp3 file is in the folder. Heres the code:

Imports TagLib
Imports System.IO

Public Class Form2
    Dim folderpb As New PictureBox
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For Each folder As String In IO.Directory.GetDirectories("C:\Users\Ranil\Music")

            folderpb.Image = My.Resources._0
            folderpb.Tag = folder
            folderpb.BackColor = Color.White
            folderpb.Size = New System.Drawing.Size(159, 141)
            AddHandler folderpb.Click, AddressOf folderpb_Click
            FlowLayoutPanel1.Controls.Add(folderpb)
        Next

    End Sub

    Private Sub folderpb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each song As String In IO.Directory.GetDirectories(folderpb.Tag & "\", "*.mp3")
            Dim filepath As String = song
            Dim file = TagLib.File.Create(filepath)



            Dim art As New PictureBox
            art.Size = New System.Drawing.Size(159, 141)

            If file.Tag.Pictures.Length >= 1 Then
                Dim bin As Byte() = DirectCast(file.Tag.Pictures(0).Data.Data, Byte())
                art.Image = Image.FromStream(New MemoryStream(bin)).GetThumbnailImage(159, 141, Nothing, System.IntPtr.Zero)
            End If
            FlowLayoutPanel1.Controls.Remove(folderpb)
            FlowLayoutPanel1.Controls.Add(art)
        Next
    End Sub
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: load picturebox in flowlayoutpanel for each folder in a directory.

#2 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6697
  • View blog
  • Posts: 27,502
  • Joined: 12-December 12

Re: load picturebox in flowlayoutpanel for each folder in a directory.

Posted 05 August 2015 - 10:22 AM

What's wrong with your code? Are there errors? What does it do or not do?

You need to ask a specific question, not just "I want to do this" and a code dump.
Was This Post Helpful? 0
  • +
  • -

#3 ranilwallace123   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 09-April 15

Re: load picturebox in flowlayoutpanel for each folder in a directory.

Posted 07 August 2015 - 11:50 AM

THe problem is onlly one picturebox shows. I want to load all the folders in the directory in the flp and then when a user clicks one of them the flp clears and then adds the files that are in the folder clicked but nothing happens
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1532
  • View blog
  • Posts: 3,859
  • Joined: 01-February 13

Re: load picturebox in flowlayoutpanel for each folder in a directory.

Posted 07 August 2015 - 01:29 PM

Well, i don`t have the TagLib library or its code so, i can`t test if thar part of the code is right. However, i believe the problem is because you are using the same class scoped PictureBox (folderpb) to add each folder. You need to move the class scoped declaration inside the For Each loop so that a New PictureBox is created for each folder.

        For Each folder As String In IO.Directory.GetDirectories("C:\Users\Ranil\Music")

            Dim folderpb As New PictureBox 'Create a New PictureBox for each folder

            folderpb.Image = My.Resources._0
            folderpb.Tag = folder
            folderpb.BackColor = Color.White
            folderpb.Size = New System.Drawing.Size(159, 141)
            AddHandler folderpb.Click, AddressOf folderpb_Click
            FlowLayoutPanel1.Controls.Add(folderpb)
        Next



Then, inside the folderpb_Click event sub you want to cast the "sender" Object to a PictureBox. That will then be a reference to the PictureBox that was clicked and raised the event.

You would use the Tag property of that PictureBox to get the Folder path from. I should also mention that the Tag property is an Object Type and needs to be converted back to a String type. You can do that with the CStr() operator, or you can use the ToString method as i show here.
    Private Sub folderpb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        'Cast the "sender" Object to a PictureBox in order to get the PictureBox that was clicked and raised this event
        Dim fldrpb As PictureBox = DirectCast(sender, PictureBox)

        'use the PictureBox that raised the event to get the Tag from.  PS - The Tag property is
        'Anchor Object Type so,  you need to convert it back to a String
        For Each song As String In IO.Directory.GetDirectories(fldrpb.Tag.ToString & "\", "*.mp3")
            Dim filepath As String = song
            Dim file = TagLib.File.Create(filepath)



            Dim art As New PictureBox
            art.Size = New System.Drawing.Size(159, 141)

            If file.Tag.Pictures.Length >= 1 Then
                Dim bin As Byte() = DirectCast(file.Tag.Pictures(0).Data.Data, Byte())
                art.Image = Image.FromStream(New MemoryStream(bin)).GetThumbnailImage(159, 141, Nothing, System.IntPtr.Zero)
            End If
            FlowLayoutPanel1.Controls.Remove(fldrpb)
            FlowLayoutPanel1.Controls.Add(art)
        Next
    End Sub



That would correct the problem of getting the selected picturebox and getting the string from the Tag property.

This post has been edited by IronRazer: 07 August 2015 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#5 ranilwallace123   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 09-April 15

Re: load picturebox in flowlayoutpanel for each folder in a directory.

Posted 07 August 2015 - 02:55 PM

Ok thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1