Visual-studio – ASP.NET Display Selected Items from Listbox in Textbox

asp.netlistboxloopstextboxvisual studio

I'm trying to display all selected items from a listbox into a textbox. Currently I'm doing the following without success:

For i As Integer = 0 To lb_words.ListCount
    If lb_words.Selected(i) = True Then
        tb_text.Text &= " Presto"
    End If
Next

What should be happening is that for every selected item in my listbox (lb.words) I want it to be appended to my textbox. So say my listbox contains Apple, Orange and Banana and I select Apple and Banana, my textbox text should read "Apple Banana"…

I've just introduced myself to ASP.NET so keep things simple 😀 Thanks.

Best Answer

Try this:

Dim s as String = ""

For each x as ListItem in lb_words.Items
     if x.Selected Then s &= x.Text & " "
Next
Related Topic