Mark Read Well-Known Member Licensed User Longtime User Mar 22, 2016 #1 Assuming I have a Listview, when the user selects an item, I want to delete the item from the listview. Code: Code: Copy Sub dListView1_SelectedIndexChanged(Index As Int) dListView1.Items.RemoveAt(Index) End Sub This code fails but the reason was not clear at the first check. Running in debug mode shows the problem. The user selects a value, the sub is called and the item is deleted. So far so good but deleteing the item causes a SelectedIndexChanged event where the index is now -1. The code crashes. My solution, consume the event: Code: Copy Sub dListView1_SelectedIndexChanged(Index As Int) If Index=-1 Then Return dListView1.Items.RemoveAt(Index) End Sub Would seem to work but still caused problems. The better solution, store the values in a second list. Add the list to the listview as required. Code: Copy Sub dListView1_SelectedIndexChanged(Index As Int) If Index=-1 Then Return MimeList.RemoveAt(Index) dListView1.Items.Clear dListView1.Items.AddAll(MimeList) End Sub This works without any problems. My question: Is this behaviour normal for a listview or is this bad programming on my behalf?
Assuming I have a Listview, when the user selects an item, I want to delete the item from the listview. Code: Code: Copy Sub dListView1_SelectedIndexChanged(Index As Int) dListView1.Items.RemoveAt(Index) End Sub This code fails but the reason was not clear at the first check. Running in debug mode shows the problem. The user selects a value, the sub is called and the item is deleted. So far so good but deleteing the item causes a SelectedIndexChanged event where the index is now -1. The code crashes. My solution, consume the event: Code: Copy Sub dListView1_SelectedIndexChanged(Index As Int) If Index=-1 Then Return dListView1.Items.RemoveAt(Index) End Sub Would seem to work but still caused problems. The better solution, store the values in a second list. Add the list to the listview as required. Code: Copy Sub dListView1_SelectedIndexChanged(Index As Int) If Index=-1 Then Return MimeList.RemoveAt(Index) dListView1.Items.Clear dListView1.Items.AddAll(MimeList) End Sub This works without any problems. My question: Is this behaviour normal for a listview or is this bad programming on my behalf?
ilan Expert Licensed User Longtime User Mar 22, 2016 #2 why are you using a different sub for that? just call dListView1.RemoveAt(Index) EDIT: when or were do you call the remove process? Last edited: Mar 22, 2016 Upvote 0
why are you using a different sub for that? just call dListView1.RemoveAt(Index) EDIT: when or were do you call the remove process?
Mark Read Well-Known Member Licensed User Longtime User Mar 22, 2016 #3 why are you using a different sub for that? Click to expand... Not sure what you mean here. ilan said: just call dListView1.RemoveAt(Index) Click to expand... RemoveAt directly is not available!! Only Items.RemoveAt. Upvote 0
why are you using a different sub for that? Click to expand... Not sure what you mean here. ilan said: just call dListView1.RemoveAt(Index) Click to expand... RemoveAt directly is not available!! Only Items.RemoveAt.
Cableguy Expert Licensed User Longtime User Mar 22, 2016 #4 I had a similar issue in m'y B4X Laucher... I just encapsulated thé code in a try/catch Upvote 0
ilan Expert Licensed User Longtime User Mar 22, 2016 #5 sorry misunderstood your issue, try this (MouseClicked event): Code: Copy Sub dListView1_MouseClicked (EventData As MouseEvent) If dListView1.SelectedIndex < 0 Then Return dListView1.Items.RemoveAt(dListView1.SelectedIndex) End Sub Last edited: Mar 22, 2016 Upvote 0
sorry misunderstood your issue, try this (MouseClicked event): Code: Copy Sub dListView1_MouseClicked (EventData As MouseEvent) If dListView1.SelectedIndex < 0 Then Return dListView1.Items.RemoveAt(dListView1.SelectedIndex) End Sub
ilan Expert Licensed User Longtime User Mar 22, 2016 #6 btw i see you are from austria, i lived for about 14 years in vienna, my parents still live there. nice place Upvote 0
btw i see you are from austria, i lived for about 14 years in vienna, my parents still live there. nice place
Mark Read Well-Known Member Licensed User Longtime User Mar 22, 2016 #7 I tried the MouseClicked event but the eventdata has no selectedindex???? I am in Leoben, Steiermark, 2 hours drive from Vienna. Upvote 0
I tried the MouseClicked event but the eventdata has no selectedindex???? I am in Leoben, Steiermark, 2 hours drive from Vienna.
ilan Expert Licensed User Longtime User Mar 22, 2016 #8 mark35at said: I tried the MouseClicked event but the eventdata has no selectedindex???? I am in Leoben, Steiermark, 2 hours drive from Vienna. Click to expand... the selectedindex is part of the listview not Eventdata Upvote 0
mark35at said: I tried the MouseClicked event but the eventdata has no selectedindex???? I am in Leoben, Steiermark, 2 hours drive from Vienna. Click to expand... the selectedindex is part of the listview not Eventdata
ilan Expert Licensed User Longtime User Mar 22, 2016 #9 if you copy the code as is it should work Upvote 0
ilan Expert Licensed User Longtime User Mar 22, 2016 #10 i had a handy store in Graz (Steiermark) the name was "Handyline" but i dont remember the street name. Upvote 0
i had a handy store in Graz (Steiermark) the name was "Handyline" but i dont remember the street name.
Mark Read Well-Known Member Licensed User Longtime User Mar 22, 2016 #11 ilan said: the selectedindex is part of the listview not Eventdata Click to expand... You are correct and it works just as well! Many thanks. I did not realise that the selectedindex is still visible during a mouse click. Upvote 0
ilan said: the selectedindex is part of the listview not Eventdata Click to expand... You are correct and it works just as well! Many thanks. I did not realise that the selectedindex is still visible during a mouse click.