Results 1 to 15 of 15

Thread: [RESOLVED] Call song files to List Box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Location
    Bandung
    Posts
    111

    Resolved [RESOLVED] Call song files to List Box

    Hi Member,
    I need some help from you here.
    Today, I started to make a MP3 player, so far I have made it work if I use a Drive List Box and Dir List Box.
    I tried it without using Drive List Box and Dir List Box but it is not working.
    My question is how can I added or called song files to Listbox directly by use List Box and Command Button

    Here is my code to call song files to ListBox.

    Code:
     Private Sub Add_Song_File_Click()
    Dim i As Integer
    
        Cdlog1.ShowOpen
        Cdlog1.Filter = "All Files (*.mp3*)|*.*"
        
    For i = 0 To MyPlayList.ListCount - 1
        'MyPlayList.Pattern = "*.mp3;*.wav"
        MyPlayList.Selected(i) = True
    
     
    Next
    End Sub
    Baj.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Call song files to List Box

    Well if you want to use a listbox rather than a file list box then you have to write some code to get the file names and add them to the list box. The Dir$() function can help you with this.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Location
    Bandung
    Posts
    111

    Re: Call song files to List Box

    Quote Originally Posted by DataMiser View Post
    Well if you want to use a listbox rather than a file list box then you have to write some code to get the file names and add them to the list box. The Dir$() function can help you with this.
    DataMiser,
    I write for a ListBox to added it use a" MyPlayList.Selected(i) = True", If I used I can added it MyPlayList.Pattern, with this to I still can't called the song to MyPlayList, except I added Drive List Box and Dir Drive..

    But I am confused about how to call some songs to MyPlayList under a ListBox, I tried it a few times but it didn't work.

    Can you please give me some sample code on how to do it, DataMiser.


    Baj.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Call song files to List Box

    I'm not really sure what you are asking but if you want to add items to a list box then you have to write code to add each of those items one by one to the list box. You can get a list of files using dir statement. You could also use the file list box and write some code to add the items in the filelist to the listbox a simple loop to read the items in the filelist and add them to the listbox.

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Call song files to List Box

    I also don't quite understand...but, if you are wondering how to get your mp3 music files into a listbox so that you can loop through the listbox and play each song, you might try:

    Code:
    Private Sub Command1_Click()
        Dim myFile As String
        myFile = Dir(App.Path & "/music/*.mp3")  'PUT YOUR DIRECTORY PATH OF MUSIC FILES HERE 
        List1.AddItem (myFile)
        Do While myFile <> ""
            myFile = Dir
            List1.AddItem (myFile)
        Loop
    End Sub
    Sam I am (as well as Confused at times).

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Location
    Bandung
    Posts
    111

    Re: Call song files to List Box

    Quote Originally Posted by SamOscarBrown View Post
    I also don't quite understand...but, if you are wondering how to get your mp3 music files into a listbox so that you can loop through the listbox and play each song, you might try:

    Code:
    Private Sub Command1_Click()
        Dim myFile As String
        myFile = Dir(App.Path & "/music/*.mp3")  'PUT YOUR DIRECTORY PATH OF MUSIC FILES HERE 
        List1.AddItem (myFile)
        Do While myFile <> ""
            myFile = Dir
            List1.AddItem (myFile)
        Loop
    End Sub
    SOB & DataMiser,Firstly thank you for your sample code, I appreciate it SOB.As well as suggestions from DataMiser use a FileList Box then to ListBox, I already did before I sent my problem to discuss here.

    In my first post, I tried asking if it was possible to use or call a song's files to ListBox directly, I didn't know if I could or not ?.

    In this project I used Drive List Box - Dir List Box - File List Box and List Box, so far it has working well, but I tried it for a Drive List Box and Dir List Box is " Visible is False" , so when project is run that Drive List Box and Dir List Box is hidden.
    When a project is run, I can't called files songs to a File List Box, except that 2 components I put is visible then I can called it, when that 2 components is hidden I can browse and see the folder path, but I can't called it when I clicked some songs files to a Files List Box. Perhap someone here knows how to get it called when 2 components are hidden.
    I attached the sample project.

    Baj.

    Sample Call Files Song To File ListBox.zip

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Call song files to List Box

    You do not need the drive list nor the directory list unless you want to use those to select the drive and folder.
    You just have to set the path on the filelist box to the folder you want it to show. It does not matter how you get the path. It could be from a drive and directory list or it could be hard coded or it could come from a dialog, a configuration file or whatever.

    Code:
    File1.Path=MyPath
    Where MyPath is the actual location of the folder to list.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Location
    Bandung
    Posts
    111

    Re: Call song files to List Box

    Quote Originally Posted by DataMiser View Post
    You do not need the drive list nor the directory list unless you want to use those to select the drive and folder.
    You just have to set the path on the filelist box to the folder you want it to show. It does not matter how you get the path. It could be from a drive and directory list or it could be hard coded or it could come from a dialog, a configuration file or whatever.

    Code:
    File1.Path=MyPath
    Where MyPath is the actual location of the folder to list.
    DataMiser,
    That I used because I need to open the folder Path, I know if I already used it then I no need to use a command dialog to browse the Path, but how without used a command dialog if control Drive and Directory is hidden.

    In my first post, I said this is done already, if I do not hide for 2 component control Drive and Directory, what I need is to call or import some songs files into Files List Box with Drive and Directory is hidden ?.

    My code below is working if Drive and Directory is visible

    Code:
     Private Sub AddSong_Click()
    Dim MyPath As String
    
    aDir1.Path = nDrive1.Drive
    xFile1.Path = aDir1.Path                   'When this control is hide hope I can get it import the files song
    
    Clog1.ShowOpen                            ' I used commondDialog perhaps if aDir1 and nDrive1 is hide I can see and  browse 
                                                        'the path files and  then import the files song to FileListBox
    Clog1.Filter = "All Files (*.*)|*.*|"
    xFile1.Pattern = "*.mp3;*"    'Search only for mp3 format files
         
    MyPath = Dir(App.Path & "/music/*.mp3")
    MyPath = xFile1.Path         '
    
    If MyPath = aDir1 & nDrive Then
      
    End If
    End Sub
    Private Sub aDir1_Change()
    xFile1.Path = aDir1.Path     'If aDir1 is visible I can easy to find all those folder path
    End Sub
    
    
    Private Sub nDrive1_Change()
    aDir1.Path = nDrive1.Drive    'If nDrive is visible I can easy to cnhages folder path to all those Path.
    End Sub
    Please help with some example code, so make it easy for me


    Baj.

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Call song files to List Box

    Here, try this:

    Code:
    Option Explicit
    
    
    Private Const BIF_RETURNONLYFSDIRS = 1
    Private Const BIF_DONTGOBELOWDOMAIN = 2
    Private Const BIF_EDITBOX = &H10
    Private Const BIF_NEWDIALOGSTYLE = &H40
    
    
    Private Const MAX_PATH = 260
    
    
    Private Declare Function SHBrowseForFolder Lib _
    "shell32" (lpbi As BrowseInfo) As Long
    
    
    Private Declare Function SHGetPathFromIDList Lib _
    "shell32" (ByVal pidList As Long, ByVal lpBuffer _
    As String) As Long
    
    
    Private Declare Function lstrcat Lib "kernel32" _
    Alias "lstrcatA" (ByVal lpString1 As String, ByVal _
    lpString2 As String) As Long
    
    
    Private Type BrowseInfo
    hWndOwner As Long
    pIDLRoot As Long
    pszDisplayName As Long
    lpszTitle As Long
    ulFlags As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
    End Type
    
    
    
    
    Private Sub cmdBrowse_Click()
        Dim lpIDList As Long
        Dim sBuffer As String
        Dim sTitle As String
        Dim tBrowseInfo As BrowseInfo
        sTitle = "Find Directory"
        With tBrowseInfo
        .hWndOwner = Me.hWnd
        .lpszTitle = lstrcat(sTitle, "")
        .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN _
        + BIF_EDITBOX + BIF_NEWDIALOGSTYLE
        End With
        lpIDList = SHBrowseForFolder(tBrowseInfo)
        If (lpIDList) Then
            sBuffer = Space(MAX_PATH)
            SHGetPathFromIDList lpIDList, sBuffer
            sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
            MsgBox sBuffer
            Dim myFile As String
            myFile = Dir(sBuffer & "\*.mp3")
            List1.AddItem (myFile)
            Do While myFile <> ""
                myFile = Dir
                List1.AddItem (myFile)
            Loop
        End If
    End Sub
    EDIT:

    This allows one to BROWSE for a directory/folder and load the mp3 files into a listbox. Then you can loop through the listbox and play each song.

    (I THINK this is what you are trying to do (and you will not need a dirlistbox nor a drivelistbox).

    Sammi
    Last edited by SamOscarBrown; Nov 30th, 2021 at 08:27 AM.
    Sam I am (as well as Confused at times).

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Location
    Bandung
    Posts
    111

    Re: Call song files to List Box

    Quote Originally Posted by SamOscarBrown View Post
    Here, try this:

    Code:
    Option Explicit
    
    
    Private Const BIF_RETURNONLYFSDIRS = 1
    Private Const BIF_DONTGOBELOWDOMAIN = 2
    Private Const BIF_EDITBOX = &H10
    Private Const BIF_NEWDIALOGSTYLE = &H40
    
    
    Private Const MAX_PATH = 260
    
    
    Private Declare Function SHBrowseForFolder Lib _
    "shell32" (lpbi As BrowseInfo) As Long
    
    
    Private Declare Function SHGetPathFromIDList Lib _
    "shell32" (ByVal pidList As Long, ByVal lpBuffer _
    As String) As Long
    
    
    Private Declare Function lstrcat Lib "kernel32" _
    Alias "lstrcatA" (ByVal lpString1 As String, ByVal _
    lpString2 As String) As Long
    
    
    Private Type BrowseInfo
    hWndOwner As Long
    pIDLRoot As Long
    pszDisplayName As Long
    lpszTitle As Long
    ulFlags As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
    End Type
    
    
    
    
    Private Sub cmdBrowse_Click()
        Dim lpIDList As Long
        Dim sBuffer As String
        Dim sTitle As String
        Dim tBrowseInfo As BrowseInfo
        sTitle = "Find Directory"
        With tBrowseInfo
        .hWndOwner = Me.hWnd
        .lpszTitle = lstrcat(sTitle, "")
        .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN _
        + BIF_EDITBOX + BIF_NEWDIALOGSTYLE
        End With
        lpIDList = SHBrowseForFolder(tBrowseInfo)
        If (lpIDList) Then
            sBuffer = Space(MAX_PATH)
            SHGetPathFromIDList lpIDList, sBuffer
            sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
            MsgBox sBuffer
            Dim myFile As String
            myFile = Dir(sBuffer & "\*.mp3")
            List1.AddItem (myFile)
            Do While myFile <> ""
                myFile = Dir
                List1.AddItem (myFile)
            Loop
        End If
    End Sub
    EDIT:

    This allows one to BROWSE for a directory/folder and load the mp3 files into a listbox. Then you can loop through the listbox and play each song.

    (I THINK this is what you are trying to do (and you will not need a dirlistbox nor a drivelistbox).

    Sammi
    SOB,
    I tried your code on my IDE, that works well and is great, this what I need, thank you so much for helping me and solving my problem.
    Just wanted to know, I tried adding a File List Box and I wrote "File1.ListIndex or Index or List and it didn't work.

    I suggest that your post on this thread put on a codebank

    Baj.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Location
    Bandung
    Posts
    111

    Re: Call song files to List Box

    Quote Originally Posted by SamOscarBrown View Post
    Here, try this:

    Code:
    Option Explicit
    
    
    Private Const BIF_RETURNONLYFSDIRS = 1
    Private Const BIF_DONTGOBELOWDOMAIN = 2
    Private Const BIF_EDITBOX = &H10
    Private Const BIF_NEWDIALOGSTYLE = &H40
    
    
    Private Const MAX_PATH = 260
    
    
    Private Declare Function SHBrowseForFolder Lib _
    "shell32" (lpbi As BrowseInfo) As Long
    
    
    Private Declare Function SHGetPathFromIDList Lib _
    "shell32" (ByVal pidList As Long, ByVal lpBuffer _
    As String) As Long
    
    
    Private Declare Function lstrcat Lib "kernel32" _
    Alias "lstrcatA" (ByVal lpString1 As String, ByVal _
    lpString2 As String) As Long
    
    
    Private Type BrowseInfo
    hWndOwner As Long
    pIDLRoot As Long
    pszDisplayName As Long
    lpszTitle As Long
    ulFlags As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
    End Type
    
    
    
    
    Private Sub cmdBrowse_Click()
        Dim lpIDList As Long
        Dim sBuffer As String
        Dim sTitle As String
        Dim tBrowseInfo As BrowseInfo
        sTitle = "Find Directory"
        With tBrowseInfo
        .hWndOwner = Me.hWnd
        .lpszTitle = lstrcat(sTitle, "")
        .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN _
        + BIF_EDITBOX + BIF_NEWDIALOGSTYLE
        End With
        lpIDList = SHBrowseForFolder(tBrowseInfo)
        If (lpIDList) Then
            sBuffer = Space(MAX_PATH)
            SHGetPathFromIDList lpIDList, sBuffer
            sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
            MsgBox sBuffer
            Dim myFile As String
            myFile = Dir(sBuffer & "\*.mp3")
            List1.AddItem (myFile)
            Do While myFile <> ""
                myFile = Dir
                List1.AddItem (myFile)
            Loop
        End If
    End Sub
    EDIT:

    This allows one to BROWSE for a directory/folder and load the mp3 files into a listbox. Then you can loop through the listbox and play each song.

    (I THINK this is what you are trying to do (and you will not need a dirlistbox nor a drivelistbox).

    Sammi
    SOB,
    ## (I THINK this is what you are trying to do (and you will not need a dirlistbox nor a drivelistbox).
    - Yes it's true, when I can import those files songs (mp3) and load them to List Box, so I do not need those controls, it means I load it directly and puth the songs files to ListBox.

    ##This allows one to BROWSE for a directory/folder and load the mp3 files into a listbox. Then you can loop through the listbox and play each song.
    - Well noted.

    Baj.

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: [RESOLVED] Call song files to List Box

    Not my code...(I'm not a very good coder!), BUT, I did locate this on this Forum. Sorry I did not include the original poster of it...I should have. So, here it is:

    See RhinoBull's post # 2:

    https://www.vbforums.com/showthread....-path-resolved
    Sam I am (as well as Confused at times).

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Location
    Bandung
    Posts
    111

    Thumbs up Re: [RESOLVED] Call song files to List Box

    Quote Originally Posted by SamOscarBrown View Post
    Not my code...(I'm not a very good coder!), BUT, I did locate this on this Forum. Sorry I did not include the original poster of it...I should have. So, here it is:

    See RhinoBull's post # 2:

    https://www.vbforums.com/showthread....-path-resolved
    SOB,
    I just refer to your link, I found that thread about this, that from RB.

    Most important for me is that you have helped me to solve my problem, for me I didn't care where you found that code, you are a good man, trying to help someone and each other.


    Baj.

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: [RESOLVED] Call song files to List Box

    FWIW (in case this is for a little "Music-Management-App")...

    If you have progressed a bit with your VB-skills, you might want to re-work the whole thing,
    based on a DB-approach (where you'd have full-text-search, sorting etc.)

    There's more Properties to store for a song-file, than just a filename -
    and a DB-record which later gets visualized in a Grid (and not a simple List), will account for that.

    I've posted a WireFrame-Demo for that a few years ago:
    https://www.vbforums.com/showthread....e-MSHFlexGrid)

    Olaf

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Location
    Bandung
    Posts
    111

    Re: [RESOLVED] Call song files to List Box

    Quote Originally Posted by Schmidt View Post
    FWIW (in case this is for a little "Music-Management-App")...

    If you have progressed a bit with your VB-skills, you might want to re-work the whole thing,
    based on a DB-approach (where you'd have full-text-search, sorting etc.)

    There's more Properties to store for a song-file, than just a filename -
    and a DB-record which later gets visualized in a Grid (and not a simple List), will account for that.

    I've posted a WireFrame-Demo for that a few years ago:
    https://www.vbforums.com/showthread....e-MSHFlexGrid)

    Olaf
    Schmidt,
    Well okay, I am not sure I can do.




    Baj.

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