Error when removing items from playlist
The code below works fine if you are deleting something other than lowest item on the listbox. If you do, then you get an error saying "Specified argument was out of the range of valid values. Parameter name: '1' is not a valid value for 'value'."
VB Code:
On Error GoTo TheError
Dim index As Integer
If playlist.Items.Count < 1 Then
MsgBox("A track must be in the playlist in order to use the remove function!", MsgBoxStyle.Exclamation, "Error")
Else
playlistPath.SelectedIndex = playlist.SelectedIndex
If MP3.MP3Playing = False Then
index = playlistPath.SelectedIndex
If playlistPath.SelectedItem = MP3.MP3File Then
MP3.MP3Stop()
tmrTrack.Enabled = False
lblPaused.Visible = False
Me.startTime = Date.Now
Me.tmrTrack.Stop()
etime.Text = "-:--"
rtime.Text = "-:--"
ttime.Text = "-:--"
WP.Text = ""
trkSeek.Value = 0
playlistPath.Items.Remove(playlistPath.SelectedItem)
playlist.Items.Remove(playlist.SelectedItem)
lblPaused.Visible = False
btnPause.Enabled = False
mnuPause.Enabled = False
btnPause.Enabled = False
mnuPause.Enabled = False
btnPause.Visible = True
mnuPause.Visible = True
btnResume.Enabled = False
mnuResume.Enabled = False
btnResume.Visible = False
mnuResume.Visible = False
btnStop.Enabled = False
mnuStop.Enabled = False
playlist.SelectedIndex = index
playlist.Update()
playlistPath.Update()
ElseIf playlistPath.SelectedItem <> MP3.MP3File Then
index = playlistPath.SelectedIndex
playlistPath.SelectedIndex = playlist.SelectedIndex
playlistPath.Items.Remove(playlistPath.SelectedItem)
playlist.Items.Remove(playlist.SelectedItem)
playlist.SelectedIndex = index
playlist.Update()
playlistPath.Update()
End If
Else
index = playlistPath.SelectedIndex
playlistPath.SelectedItem = playlist.SelectedItem
playlistPath.Items.Remove(playlistPath.SelectedItem)
playlist.Items.Remove(playlist.SelectedItem)
playlist.Update()
playlistPath.Update()
playlist.SelectedIndex = index
End If
End If
Exit Sub
TheError: MsgBox(Err.Description, , " Error")
Re: Error when removing items from playlist
Ouch, old style error handling.
Which line throws the error?
I've would guess it is one of the playlist.SelectedIndex=index ones.
After all, just before those you call Remove. Once that has been called, that statement may not work quite as you'd like.
Re: Error when removing items from playlist
I agree with Shaggy that you should try to learn to use structured exception handling ASAP. With reagrds to your error, I'm quite sure that if you set a breakpoint and Watch your variables, specifically index and playList.Items.Count, you'll find the problem pretty quickly.
Re: Error when removing items from playlist
Fixed it:
VB Code:
On Error GoTo TheError
Dim index As Integer
If playlist.Items.Count < 1 Then
MsgBox("A track must be in the playlist in order to use the remove function!", MsgBoxStyle.Exclamation, "Error")
Else
playlistPath.SelectedIndex = playlist.SelectedIndex
If MP3.MP3Playing = False Then
index = playlistPath.SelectedIndex
If playlistPath.SelectedItem = MP3.MP3File Then
MP3.MP3Stop()
tmrTrack.Enabled = False
lblPaused.Visible = False
Me.startTime = Date.Now
Me.tmrTrack.Stop()
etime.Text = "-:--"
rtime.Text = "-:--"
ttime.Text = "-:--"
WP.Text = ""
trkSeek.Value = 0
playlistPath.Items.Remove(playlistPath.SelectedItem)
playlist.Items.Remove(playlist.SelectedItem)
lblPaused.Visible = False
btnPause.Enabled = False
mnuPause.Enabled = False
btnPause.Enabled = False
mnuPause.Enabled = False
btnPause.Visible = True
mnuPause.Visible = True
btnResume.Enabled = False
mnuResume.Enabled = False
btnResume.Visible = False
mnuResume.Visible = False
btnStop.Enabled = False
mnuStop.Enabled = False
If index < playlist.Items.Count Then
playlist.SelectedIndex = index
ElseIf playlist.SelectedIndex = playlist.Items.Count Then
playlist.SelectedIndex = index - 1
End If
playlist.Update()
playlistPath.Update()
ElseIf playlistPath.SelectedItem <> MP3.MP3File Then
playlistPath.SelectedIndex = index
playlistPath.SelectedIndex = playlist.SelectedIndex
playlistPath.Items.Remove(playlistPath.SelectedItem)
playlist.Items.Remove(playlist.SelectedItem)
If index < playlist.Items.Count Then
playlist.SelectedIndex = index
ElseIf index = playlist.Items.Count Then
playlist.SelectedIndex = index - 1
End If
playlist.Update()
playlistPath.Update()
End If
Else
index = playlistPath.SelectedIndex
playlistPath.SelectedItem = playlist.SelectedItem
playlistPath.Items.Remove(playlistPath.SelectedItem)
playlist.Items.Remove(playlist.SelectedItem)
playlist.Update()
playlistPath.Update()
playlist.SelectedIndex = index
End If
End If
Exit Sub
TheError: MsgBox(Err.Description, , " Error")