-
Oct 3rd, 2014, 03:54 AM
#1
Thread Starter
New Member
Listbox item select and deselect event
Hi guys,
I have a listbox which can be multi-selected. By default, when the user clicks on an item, it is selected. And when the user clicks on the same item again, it is deselected.
Instead of the SelectedIndexChanged event which fires when an item is selected and deselected, I want to determine whether the item is selected or deselected.
Is there such event in vb?
-
Oct 3rd, 2014, 08:50 AM
#2
Re: Listbox item select and deselect event
Instead of the SelectedIndexChanged event which fires when an item is selected and deselected, I want to determine whether the item is selected or deselected.
The very first thing you should do before asking that question is look up the documentation. I Googled "vb.net ListBox events" and it led me to the ListBox Events MSDN documentation.
After reviewing the list of events for the ListBox I did not find an event that fires when an item is selected or deselected.
-
Oct 3rd, 2014, 10:57 AM
#3
Thread Starter
New Member
Re: Listbox item select and deselect event
Originally Posted by dday9
The very first thing you should do before asking that question is look up the documentation. I Googled "vb.net ListBox events" and it led me to the ListBox Events MSDN documentation.
After reviewing the list of events for the ListBox I did not find an event that fires when an item is selected or deselected.
Yes i did tried to google, but I only managed to find this from MSDN
http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx
Didn't understand the code, that's why I asked. Thanks for the reply though, so I guess I have try another way.
-
Oct 3rd, 2014, 12:47 PM
#4
Re: Listbox item select and deselect event
That event is for WPF programs and not related to Windows Forms applications, which is probably why you didn't understand the code. There is no other event in a Windows Forms ListBox that determent exactly which item that has been selected or unselected except from the SelectedXXXChanged events. You need to use the SelectedIndexChanged and loop through the SelectedItems collection to see which item(s) that is/are selected.
Last edited by Joacim Andersson; Oct 3rd, 2014 at 12:50 PM.
-
Oct 3rd, 2014, 01:44 PM
#5
Re: Listbox item select and deselect event
You need to use the SelectedIndexChanged and loop through the SelectedItems collection to see which item(s) that is/are selected.
This does bring up a point that I didn't think of before. You could create a class that inherits a ListBox with a custom events to signal which item was selected/deselected. The only thing that could probably trip it up is the fact that you can select multiple items in a ListBox, but even then it could still be managed.
-
Oct 4th, 2014, 02:17 AM
#6
Thread Starter
New Member
Re: Listbox item select and deselect event
Originally Posted by Joacim Andersson
That event is for WPF programs and not related to Windows Forms applications, which is probably why you didn't understand the code. There is no other event in a Windows Forms ListBox that determent exactly which item that has been selected or unselected except from the SelectedXXXChanged events. You need to use the SelectedIndexChanged and loop through the SelectedItems collection to see which item(s) that is/are selected.
Originally Posted by dday9
This does bring up a point that I didn't think of before. You could create a class that inherits a ListBox with a custom events to signal which item was selected/deselected. The only thing that could probably trip it up is the fact that you can select multiple items in a ListBox, but even then it could still be managed.
Alright I will figure that out, will ask questions if I have doubts again.
Now I have another problem, my listbox items are taken from an arraylist. When I click on the clear button, I want to remove the selected items from the array. So here goes:
Code:
Private Sub btnClearSel_Click(sender As Object, e As EventArgs) Handles btnClearSel.Click
Dim index As Integer
For Each index In listPoles.SelectedIndices
polesArrayX.RemoveAt(index)
Next
End Sub
It works if I only select 1 item, and I get this error if I selected more than 1 "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection."
Any idea what went wrong?
-
Oct 4th, 2014, 07:15 AM
#7
Re: Listbox item select and deselect event
Yes, let's suppose that you have 10 items in your listbox and subsequently you also have 10 items in your array. They are index from 0 to 9. Now you select the first and the last item in the listbox (index 0 and 9) what do you think will happen when you go into the loop you posted above? You remove the first item from the array and then you want to remove the item with index 9, but since you've already removed one item there are only 9 left which are now indexed 0 to 8, so index 9 will cause an index out of range exception.
The solution is to loop backwards, to first remove the last item before you remove the first. So you can't use a for each loop. You need a regular for loop.
Code:
For i As Integer = listPoles.SelectedIndicies.Count - 1 To 0 Step -1
polesArrayX.RemoveAt(listPoles.SelectedIndicies(i))
Next
-
Oct 4th, 2014, 12:19 PM
#8
Thread Starter
New Member
Re: Listbox item select and deselect event
Originally Posted by Joacim Andersson
Yes, let's suppose that you have 10 items in your listbox and subsequently you also have 10 items in your array. They are index from 0 to 9. Now you select the first and the last item in the listbox (index 0 and 9) what do you think will happen when you go into the loop you posted above? You remove the first item from the array and then you want to remove the item with index 9, but since you've already removed one item there are only 9 left which are now indexed 0 to 8, so index 9 will cause an index out of range exception.
The solution is to loop backwards, to first remove the last item before you remove the first. So you can't use a for each loop. You need a regular for loop.
Code:
For i As Integer = listPoles.SelectedIndicies.Count - 1 To 0 Step -1
polesArrayX.RemoveAt(listPoles.SelectedIndicies(i))
Next
Awesome, that works! Didn't think of it, thank you sir!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|