Anyone know of a smooth way to get all of the selected items in a listbox control by using extension methods?
And, please, spare me the argument of it's irrelevant as to how one gets such a list because in the end everything uses a loop to iterate over the items and find the selected ones anyway.
Hello i've created one solution for this problem in this post using VB.NET:
Getting all selected values from an ASP ListBox
This code below is the same as the link above:
I hope it helps.
Extension method:
You can call it on your listbox like:
You could also do the conversion using a 'Cast' on the list box items like:
Not sure which will perform better
OfType
orCast
(my hunch isCast
).Edit based on Ruben's feedback for a generic ListControl method which would indeed make it much more useful extension method: