[HELP] VB jumping out of private sub and skipping the last few lines of codes

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
ok so i think the title is pretty straight forward but let me explain what im trying to accomplish here:
i have 4 listview controls like so:
musicPlaylist - musicPlaylistHidden
musicExplorerList - musicExplorerListHidden

basically the explorer list displays all the mp3 files in a specific folder on the computer and then i can drag & drop what songs i want in the playlist. the 2 hidden playlists are for the urls of the songs in the first 2 lists
and so, all works fine except that in the playlist when i want to rearrange songs it skips the last 2 lines in an if statement. here is the code:
<pre class="prettyprint lang-vb Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles musicPlayList.DragDrop, musicExplorerList.DragDrop
Dim Listview As ListView = DirectCast(sender, ListView)
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = CType(e.Data.GetData("System.Windows.Forms.ListViewItem()"), ListViewItem())
Dim i As Integer = 0
Dim pt As Point = Listview.PointToClient(New Point(e.X, e.Y))
Dim drop_index As Integer

get the drop index based on cursor position within listview
If Listview.GetItemAt(pt.X, pt.Y) Is Nothing Then
If Listview.Items.Count = 0 Then
drop_index = 0
Else
drop_index = Listview.Items.Count
End If
Else
drop_index = Listview.GetItemAt(pt.X, pt.Y).Index
End If

add each selected item
For Each myItem In myItems
Dim Item As ListViewItem = CType(myItems(i).Clone, ListViewItem)

If Listview.Name = "musicPlayList" Then
If l = 0 Then
Dim index As Integer = musicExplorerList.Items.IndexOf(musicExplorerList.SelectedItems(0))
Dim item2 As ListViewItem = musicExplorerListHidden.Items(index)
musicExplorerListHidden.Items.Remove(musicExplorerListHidden.Items(index))
musicPlayListHidden.Items.Insert(drop_index, item2)
musicExplorerList.Items.Remove(musicExplorerList.Items(index))
Listview.Items.Insert(drop_index, Item)
Else
Dim index As Integer = musicPlayList.Items.IndexOf(musicPlayList.SelectedItems(0))
Dim item2 As ListViewItem = musicPlayListHidden.Items(index)
musicPlayListHidden.Items.Remove(musicPlayListHidden.Items(index))
musicPlayListHidden.Items.Insert(drop_index, item2)
musicPlayList.Items.Remove(musicPlayList.Items(index))
Listview.Items.Insert(drop_index, Item)
End If
Else
Dim index As Integer = musicPlayList.Items.IndexOf(musicPlayList.SelectedItems(0))
Dim item2 As ListViewItem = musicPlayListHidden.Items(index)
musicPlayListHidden.Items.Remove(musicPlayListHidden.Items(index))
musicExplorerListHidden.Items.Insert(drop_index, item2)
musicPlayList.Items.Remove(musicPlayList.Items(index))
Listview.Items.Insert(drop_index, Item)
End If
i = i + 1
Next

End Sub[/code]
<br/>
so basically when it gets to the "else" under l = 0 the code jumps out of the if statement right before it gets to those 2 lines:
<pre class="prettyprint lang-vb" style="font-size:12.222222328186035px musicPlayList.Items.Remove(musicPlayList.Items(index))
Listview.Items.Insert(drop_index, Item)[/code]
also it doesnt only jump out of the if statement but it randomly jumps either at a timer i have on the form or to another sub
i need help understanding why this happens because it is very weird. everything works well and then when it gets to those 2 lines it just jumps out of the if statement

thanks in advance

View the full article
 
Back
Top