4

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 :D Thanks.

2
  • Something possibly related to your problem - MsgBox won't work in server side code. It won't display a message on the client, and it sure as heck won't display a message on the server :-) Commented May 17, 2009 at 7:02
  • ok, i removed the messagebox stuff... I was more focusing on the loop than the result... seeing as my loops are the problem Commented May 18, 2009 at 0:30

7 Answers 7

3

Try this:

Dim s as String = ""

For each x as ListItem in lb_words.Items
     if x.Selected Then s &= x.Text & " "
Next
Sign up to request clarification or add additional context in comments.

5 Comments

Using a StringBuilder would be more efficient.
yes, but he asked for simple, and appending to a string is simpler than dealing with String builders
This code looks promising, however I'm getting errors for 'ListBoxItem' amd 'x.isselected'... should I note that I've got Option Explicit and String On? ListBoxItem error gives me the option to replace it with ListItem or ListBox
@tmhai - the IsSelected should be Selected. and listboxitem should have been ListItem. I blame the errors on it being midnight
my only options w/ variable.selected are .selecteditem, .selectedcvalue, or .selectedindex...
3
        foreach (ListItem Mail in ListBox1.Items)
        {
            if (Mail.Selected)
            {
                Mail.Selected = true+",";
                mail.To.Add(Mail.ToString());                                   
            }

Comments

1

aspx page:

   <asp:ListBox ID="myList" runat="server" SelectionMode="Multiple">
        <asp:ListItem>Apple</asp:ListItem>
        <asp:ListItem>Orange</asp:ListItem>
        <asp:ListItem>Grapes</asp:ListItem>
   </asp:ListBox>
   <br/>
   <asp:TextBox id="myText" runat="server"></asp:TextBox>

codebehind (C#)

 StringBuilder sb=new StringBuilder();
        for (int i = 0; i < myList.Items.Count; i++)
            sb.Append(myList.Items[i].Selected ? myList.Items[i].Text + " " : "");
 myText.Text=sb.ToString();

Comments

1

If you are using databound listbox then try this code using button click event:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each objDataRowView As DataRowView In Me.ListBox1.SelectedItems
        Me.TextBox1.Text &= (objDataRowView("ITEM LIST").ToString() & " ; ")
        'here "ITEM LIST" is the "column name" which is used as DATASOURCE for LISTBOX1
    Next
End Sub

Comments

0

One solution would be to override the .ToString() method to concatenate all the values in your list.

1 Comment

the thing is that he doesn't want ALL the values, just the values that have been selected by the user
0

You can use lb_words.SelectedItems instead of looping through all the records and finding the selected items among them. You can use the following code:

        Dim s As New StringBuilder()

        For Each item As Object In Me.lb_words.SelectedItems
            s.Append(item)
            s.Append(" ")
        Next

        Me.TextBox1.Text = s.ToString()

If you select Apple & Banana, your textbox will contain 'Apple Banana '

1 Comment

an asp.net listbox has no SelectedItems method
0
 protected void Button1_Click(object sender, EventArgs e)
{
    //to display multiple items
    String it =" ";
    foreach (ListItem item in listbox.Items)
    {
        if (item.Selected)
        {
            it = it + ", " + item.Text;
        }
        Label1.Text = it;
    }

}

1 Comment

with this code you can display multiple items instead of one from the (ListBox) web server control if you added the property (SelectionMode = "Multiple")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.