Saturday, March 31, 2012

Strang behaviour of multiselection Listbox

Hello,

i´d like to get access to the selected items of my listbox...

I did this in a loop, but I get only acces to the first selected item of the Listbox.

Has anyone solved the same problem? I really need help. Thx in advance.

Code (VB.net):

dim el as listitemfor each el in mylistbox.Items if el.selected then 'do something end ifnext

Give this a try...

 iCnt = 0 Dim li As ListItem For Each li In myListBox.Items If li.Selected Then iCnt += 1 Next If iCnt <> 0 Then Dim i As Integer iCnt = myListBox.Items.Count For i = 0 To iCnt - 1 If myListBox.Items(i).Selected Then 'Do Something End If Next
 End If
 Zath

Still the same problemSad
I´m sad...

But thank you for the try...

That shouldn't be.

Do you have this line in there?

If myListBox.Items(i).Selected Then

Zath


Yes...

No matter what I do, the first selected item returns true for .selected

the next ones return false...


ListBoxes (and CheckBoxLists) expose a new property called "GetSelectedIndices()"

So you can say

Dim LI As ListItem
For Each idx As Int32 in mylistbox.GetSelectedIndices()
LI = mylistbox.Items(idx)
'Do what you need to do with this selected list item
Next

*If* that doesn't work for you, and there is absolutely no reason why this code or the code you posted shouldn't... then you have other issues, like for instance, re-binding the list on every page load (which would "reset" the listboxbefore your event handler got a hold of the control)


I´m working on 1.1 so that the above function is not available for me...


But I will check the second issue. Thanks for the hint...


That did the trick... Sorry for the wasting of your time...

Did a wrong binding on PostBacks so I killed all of the selections except of one in the listbox...

*smashing head against the next wall*


i can't speak for the others replying on this topic... but you didn't/aren't wasting my time... glad to help out...

the whole binding cylce is a huge "gotcha" in .NET, we've all had our battles with it


(this is where you tell them "but it sure is great thatEasyListBox doesn't have that problem" Big Smile )

0 comments:

Post a Comment