dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Listbox Questions (2)

  1. #1

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,619

    Resolved [RESOLVED] Listbox Questions (2)

    First, In this little Jukebox program I'm building, the user (me) will have the option to create a Playlist (see Image). When the user clicks on Command5 ("Play Entire Playlist"), I want to start at the first item and go to the end (obviously). My code (below) does that fine. But, if the user has already clicked that button, and adds more songs to the Playlist, the operation will stop when the ORIGINAL last item is played, hence not playing any added songs.

    Second, My code deletes each song after it is played (listindex x-1)...and this works find for the entire (original) list, but does not delete the last played (as my code says "if x > 0 then"). How do I delete that last appearing song (which now has an index of 0).

    One not need to worry about the song playing, just need to figure out how to continue going through a listbox after additional items are added (but can't keep clicking the command button to do this), and how to delete that final item in the listbox.

    Code:
    Private Sub Command5_Click()    MMControl1.TimeFormat = 0
        Dim x As Integer
        Dim songPath As String
        For x = 0 To List4.ListCount - 1
            If MMControl1.Mode = mciModeNotOpen Or MMControl1.Mode = mciModeStop Then
                List4.ListIndex = x
                If x > 0 Then
                    List4.RemoveItem (x - 1)
                End If
                MMControl1.Command = "Close"
                MMControl1.Notify = False
                MMControl1.Wait = False
                MMControl1.Shareable = False
                MMControl1.DeviceType = "MpegVideo"  'plays mp3 files
                Dim mySong As String
                Dim myArtist As String
                Dim myAlbum As String
                Dim y As Integer, z As Integer
                y = InStr(1, List4.Text, " -by: ")
                mySong = Mid(List4.Text, 1, y - 1)
                z = InStr(1, List4.Text, " -Album: ")
                myArtist = Mid(List4.Text, y + 6, z - y - 6)
                myAlbum = Mid(List4.Text, z + 9)
                songPath = App.Path & "\music\" & myArtist & "\" & myAlbum & "\" & mySong & ".mp3"
                MMControl1.FileName = songPath
                MMControl1.Command = "Open"
                MMControl1.Command = "play"
                If MMControl1.CanPlay = False Then
                     MsgBox "Can't Play " & MMControl1.FileName
                End If
                While MMControl1.Mode = mciModePlay
                    DoEvents
                Wend
           End If
       Next x
    End Sub
    Attachment 162375

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,599

    Re: Listbox Questions (2)

    I am a bit confused by the loop. It looks like you are looping based on the list count but removing items from the list within the loop. I would expect an index out of bounds error at some point.

    Instead of using a for loop what about a Do While Listcount>0 ?

    As for the part where you have If X>0 what if you add an ElseIF Listcount=1?

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,371

    Re: Listbox Questions (2)

    I would throw away both loops.


    Write a subroutine that checks for ListCount > 0. If True then play the file for List(0) and RemoveItem 0, if False then clean up as needed and go idle awaiting user input events.

    To "start playing" call the subroutine.

    Each time the Done event is raised just call the subroutine again.


    Remember that any time you think you need to call DoEvents you are probably doing things wrong. "Trust the events, Luke!"

    This isn't QBasic after all.

  4. #4

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,619

    Re: Listbox Questions (2)

    Yes dile...I hardly ever, ever use DoEvents. But in this case I couldn't find a way to make the 'loop' know when to wait for the current song to finish...let me relook this with DM's and your comments...well, your SUGGESTIONS, anyway.

  5. #5

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,619

    Re: Listbox Questions (2)

    Thanks both of you...I used diletante's suggestions. Works great. Here's the code (if anyone cares).

    Code:
    Private Sub cmdPlayPlayList_Click()    If songList.SelCount > 0 Then
            Dim ans As Integer
            ans = MsgBox("Please Wait For The Album Song List To Complete", vbExclamation, "Please Wait")
            Exit Sub
        End If
        PlayMusic
    End Sub
    Private Sub PlayMusic()
        MMControl1.Command = "Close"
          If PlayListcount = True Then
            If MMControl1.Mode = mciModeNotOpen Or MMControl1.Mode = mciModeStop Then
                PlayList.ListIndex = 0
                Dim mySong As String
                Dim myArtist As String
                Dim myAlbum As String
                Dim songpath As String
                Dim y As Integer, z As Integer
                y = InStr(1, PlayList.Text, " -by: ")
                mySong = Mid(PlayList.Text, 1, y - 1)
                z = InStr(1, PlayList.Text, " -Album: ")
                myArtist = Mid(PlayList.Text, y + 6, z - y - 6)
                myAlbum = Mid(PlayList.Text, z + 9)
                songpath = App.Path & "\music\" & myArtist & "\" & myAlbum & "\" & mySong & ".mp3"
                MMControl1.FileName = songpath
                MMControl1.Command = "Open"
                MMControl1.Command = "play"
           End If
        End If
    End Sub
    
    
    Private Sub MMControl1_Done(NotifyCode As Integer)
        If NotifyCode = 1 Then
             PlayList.RemoveItem (0)
             PlayMusic
        End If
    End Sub
    Private Function PlayListcount() As Boolean
        If PlayList.ListCount > 0 Then
            PlayListcount = True
        Else
            PlayListcount = False
        End If
    End Function

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width