VB.NET - ListBox repeating list - Asked By jenn on 22-Sep-10 07:28 PM

hi guys!

am still working on this freakin media player LOL
I have 2 listboxes for the playlist (Listbox1 is hidden under Listbox2) the reason for this is Listbox1 holds the direct path of the music...and listbox 2 only shows the song name.

anyway, what I am TRYING to do is to get the list to start over from the top after it has finished playing the list. I want the list to go to the TOP of the list and play through the list again. everything I have tried does not work :(.
whenever it gets done with the LAST SONG of the list..it gives me an out of range error...

here is my code so far, can someone please help me get this mess working right???????!! (lol)

If wmp.playState = WMPPlayState.wmppsPlaying = True Then
      ElseIf wmp.playState = WMPPlayState.wmppsStopped = True Then
        Listbox1.SelectedIndex = Listbox1.SelectedIndex + 1
        ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1
        wmp.URL = Listbox1.SelectedItem
      ElseIf CheckBox1.Checked = False Then
        wmp.URL = Listbox1.SelectedItem
        'it() 's the last song in the list so either move to index 0 from here or stop the music completely
        Listbox1.SelectedIndex = 0
        ListBox2.SelectedIndex = 0
      End If
    End If
  End Sub
Rolf Jaeger replied to jenn on 22-Sep-10 08:20 PM
Hi Jenn:

it seems to me that what's missing from your code is something like this:

If ListBox1.SelectedIndex = ListBox1.Items.Count-1 Then
   ListBox1.SelectedIndex = 0 
   ListBox2.SelectedIndex = 0 
End If

Hope this helped,
Rolf
jenn replied to Rolf Jaeger on 23-Sep-10 09:57 AM
Okay, Thank you for the reply. I can see how that line might be needed...otherwise the Index=0 would be a dead futile command!

however, when I inserted that line, I now get an error when the last song in the list finishes, it tells me on this line:

Listbox1.SelectedIndex = Listbox1.SelectedIndex + 1
Argument out of range.
says that the InvalidArguement='Value of '9' is not valid for selectedIndex. Parameter Name=SelectedIndex

The value of 9 is how many songs I have in this particular list.

here is the code I am trying to use now:

This is for the play all check box
  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  
    If CheckBox1.Checked = True Then
      If wmp.playState = WMPPlayState.wmppsPlaying = True Then
      ElseIf wmp.playState = WMPPlayState.wmppsStopped = True Then
        Listbox1.SelectedIndex = Listbox1.SelectedIndex + 1
        ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1
        wmp.URL = Listbox1.SelectedItem
      ElseIf CheckBox1.Checked = False Then
        wmp.URL = Listbox1.SelectedItem
        'it() 's the last song in the list so either move to index 0 from here or stop the music completely
        If Listbox1.SelectedIndex = Listbox1.Items.Count - 1 Then
          If ListBox2.SelectedIndex = ListBox2.Items.Count - 1 Then
            Listbox1.SelectedIndex = 0
            ListBox2.SelectedIndex = 0
          End If
        End If
      End If
    End If
  End Sub
jenn replied to Rolf Jaeger on 23-Sep-10 10:19 AM
I tried moving the code around a bit to see if I can get it into a better position. I now have it like this:
If CheckBox1.Checked = True Then
      If wmp.playState = WMPPlayState.wmppsPlaying = True Then
      ElseIf wmp.playState = WMPPlayState.wmppsStopped = True Then
        Listbox1.SelectedIndex = Listbox1.SelectedIndex + 1
        ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1
        If Listbox1.SelectedIndex = Listbox1.Items.Count - 1 Then
          If ListBox2.SelectedIndex = ListBox2.Items.Count - 1 Then
            Listbox1.SelectedIndex = 0
            ListBox2.SelectedIndex = 0
          ElseIf CheckBox1.Checked = False Then
            wmp.URL = Listbox1.SelectedItem
            'it() 's the last song in the list so either move to index 0 from here or stop the music completely
            If Listbox1.SelectedIndex = Listbox1.Items.Count - 1 Then
              If ListBox2.SelectedIndex = ListBox2.Items.Count - 1 Then
                Listbox1.SelectedIndex = 0
                ListBox2.SelectedIndex = 0
              End If
            End If
          End If
        End If
      End If
    End If
  End Sub
This is no longer giving me an out of range error, however it is finishing the last song of my list and then just sitting there..it will not go to the top and play the first song :(   (and by the way LOL @ all of the END IFS....lol....6 of em!!! hehehehheee
jenn replied to Rolf Jaeger on 23-Sep-10 10:47 AM
OKAY..we are getting CLOSER! :)

Been moving it around some more and it is now looking like this:
If CheckBox1.Checked = True Then
      If wmp.playState = WMPPlayState.wmppsPlaying = True Then
      ElseIf wmp.playState = WMPPlayState.wmppsStopped = True Then
        If Listbox1.SelectedIndex = Listbox1.Items.Count - 1 Then
          If ListBox2.SelectedIndex = ListBox2.Items.Count - 1 Then
            Listbox1.SelectedIndex = 0
            ListBox2.SelectedIndex = 0
          ElseIf CheckBox1.Checked = False Then
            wmp.URL = Listbox1.SelectedItem
            'it() 's the last song in the list so either move to index 0 from here or stop the music completely 
            If Listbox1.SelectedIndex = Listbox1.Items.Count - 1 Then
              If ListBox2.SelectedIndex = ListBox2.Items.Count - 1 Then
                Listbox1.SelectedIndex = 0
                ListBox2.SelectedIndex = 0
              End If
            End If
          End If
        End If
      End If
    End If
this is not producing the out of range error...and it is actually highlighting the song at the top of the list, but it will not automatically play the song. it plays when you click on the play button..so I figure "okay, play the first song using the play button and see if the "play All" takes off from there. it doesnt LOL!!! it reaches the first song and sits there now instead of at the end of the list!
Rolf Jaeger replied to jenn on 23-Sep-10 01:51 PM
Hi Jenn:

please explain what you are trying to accomplish with this logic:

If wmp.playState = WMPPlayState.wmppsPlaying = True Then 
ElseIf wmp.playState = WMPPlayState.wmppsStopped = True Then

Thanks,
Rolf
jenn replied to Rolf Jaeger on 23-Sep-10 03:26 PM
hi Rolf and yes :) LOL (i can tell you what these are for.

If wmp.playState = WMPPlayState.wmppsPlaying = True Then 'If it has started to play
      ElseIf wmp.playState = WMPPlayState.wmppsStopped = True Then 'This is keeping the song playing until it has ended

I am now at a place where i am beginning to think its possessed LOL...this following code is playing one song after the other just fine, until it gets to the last song. When it is playing the NEXT to LAST song, it will Highlight the beginning song of the list. it will wait until the current song is finished and then it will play the last song as it should...BUT the highlighted song is the first song of the list..*sigh* SO then it will SKIP the first song and play the second song on this list..the highlighter will highlight the right song...and the list continues to play on from that point just fine. it seems to me that the trouble is solely in the transition from the end of the list to the beginning of the list

Anyways, here is the code as it sits now:
If CheckBox1.Checked = True Then
      If wmp.playState = WMPPlayState.wmppsPlaying = True Then 'If it has started to play
      ElseIf wmp.playState = WMPPlayState.wmppsStopped = True Then 'This is keeping the song playing until it has ended
        Listbox1.SelectedIndex = Listbox1.SelectedIndex + 1 'This means to move to next Song in List 
        ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1 'This means to move to next Song in List
        wmp.URL = Listbox1.SelectedItem 'This means to play the song that it has moved to
        If ListBox2.SelectedIndex = ListBox2.Items.Count - 1 Then 'This is telling us that the list is done
          If Listbox1.SelectedIndex = Listbox1.Items.Count - 1 Then 'This is telling us that the list is done
          End If
          Listbox1.SelectedIndex = 0 'This is SUPPOSE to return selectin to beginning of list
          ListBox2.SelectedIndex = 0 'This is SUPPOSE to return selectin to beginning of list
        ElseIf CheckBox1.Checked = False Then
          'it() 's the last song in the list so either move to index 0 from here or stop the music completely 
          If Listbox1.SelectedIndex = Listbox1.Items.Count - 1 Then
            If ListBox2.SelectedIndex = ListBox2.Items.Count - 1 Then
              Listbox1.SelectedIndex = 0
              ListBox2.SelectedIndex = 0
            End If
          End If
        End If
      End If
    End If
  End Sub
    
#End Region
Rolf Jaeger replied to jenn on 24-Sep-10 10:06 AM
Hi Jenn:

I asked my question about the logic you are using for two reasons.

1) Is there a reason why you are using the somewhat confusind syntax of a=b=True?
After all that's identical to 'If a=b Then' (which is considerably easier to read)

2) When the first condition is True no statements are excecuted.
If that's what you intended you could just eliminate the first If statement and start with

If wmp.playState = WMPPlayState.wmppsStopped Then 

One further comment (I think I commented on this before, in answer to one of your previous posts): it seems to me that you are writing too much code to ensure that ListBox1 and ListBox2 are synchronized (as they should). You could accomplish this with much less code clutter if you were to include the following statement into the SelectedIndexChanged event handler for ListBox1 (which, if I am not mistaken, is the only one your user interacts with):

ListBox2.SelectedIndex = ListBox1.SelectedIndex

The reason I am harping on this is that your constantly dealing with the explicit synchronization of these two listboxes makes your code VERY hard to read (and probably difficult to maintain).

In order for me to comment on your code any further you would need to remind me how (and/or when) this particular code segment is reached during the execution.

Best wishes,
Rolf
jenn replied to Rolf Jaeger on 24-Sep-10 10:35 AM
okay Rolf, those are very good points. what I will do is see how well I can clean this up and implement the changes you are hitting on..and then return to find out (lol) why this freakin thing is skipping the last song of the list and the first song of the list..*sigh* I am most sure that it is ONE of the +1 lines...that should probably read -1..

but I am sure if I clean it up as you suggested it would be far easier to figure out WHICH +1 line should be -1 LOL

okay, be back to ya after i got it much easier to read :)