2

I have a list-box, I want to loop through all the selected items and get each selected items text value.

for (int i = 0; i < lstFieldNames.selectedItems.Count; i++)
{
string s = lstFieldNames.SelectedItems[i].ToString();
}

the value of s is "{ Item = ADDR }"

I don't need the { Item = }, I just want the text "ADDR".

What am I doing wrong, I tried a few things and nothing seems to work for me.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
user1619768
  • 53
  • 1
  • 1
  • 7
  • What's the datasource of the ListBox? – Tim Schmelter Aug 23 '12 at 13:05
  • var items = (from i in xDoc.Descendants("ITEM") orderby i.Value select new {Item = i.Element("FIELDNAME").Value }).ToList(); l.DataSource = items; l.DisplayMember = "Item" – user1619768 Aug 23 '12 at 14:09
  • This is simply because the data type of items has no ToString() override (as it's created on the fly). There are two main options then AFAICS - 1) instead of using var items, create a holding class and pass the elements (or the collection as a whole) to the new class with the desired ToString override - 2) on the loop above, post process the selected item element by passing it to a function that returnms the item string and not the ToString. – Wolf5370 Aug 23 '12 at 14:40

4 Answers4

2

Well, this is a Winforms question because an ASP.NET ListBox has no SelectedItems property(notice the plural). This is important since a Winforms ListBox has no ListItems with Text and Value properties like in ASP.NET, instead it's just an Object.

You've also commented that the datasource of the ListBox is an anonymous type. You cannot cast it to a strong typed object later.

So my advice is to create a class with your desired properties:

class ListItem {
    public String Item { get; set; }
}

Create instances of it instead of using an anonymous type:

var items = (from i in xDoc.Descendants("ITEM") 
             orderby i.Value 
             select new ListItem(){ Item = i.Element("FIELDNAME").Value })
            .ToList();

Now this works:

foreach (ListItem i in lstFieldNames.SelectedItems)
{
    String item = i.Item;   
}

Note that my ListItem class is not the ASP.NET ListItem.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks a million. Once you directed me to the data source.. I seen where {Item =} was coming from. All along I thought it was the listbox i was having problems with. Thanks again. – user1619768 Aug 23 '12 at 14:35
0
string s = lstFieldNames.SelectedItems[i].Text.ToString();

Note the Text property

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
saj
  • 4,626
  • 2
  • 26
  • 25
  • 2
    But this is a winforms question, in ASP.NET there's a Text property in `ListItem` but in Winforms it's just an object. http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedobjectcollection.item.aspx – Tim Schmelter Aug 23 '12 at 13:00
  • I get an error when trying lstFieldNames.SelectedItems[i].Text; 'object' does not contain a definition for 'Text' – user1619768 Aug 23 '12 at 13:02
0

This would fetch Selected values only.

EDIT:

foreach (object listItem in listBox1.SelectedItems)
{
    string s = listItem.ToString();
}
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
  • do i need to add a directive? I get the error message - The type or namespace name "ListItem" could not be found(are you missing a using directive or an assembly reference?) – user1619768 Aug 23 '12 at 13:22
0

Use this code :

  string s = "";

  foreach(var item in lstFieldNames.SelectedItems) 
  {
     s += item.ToString() + ",";
  }

  textBox1.Text = s;

If need on one selected item from your listbox try this cod :

  string s = "";

  foreach(var item in lstFieldNames.SelectedItems) 
  {
     s = item.ToString();
  }

EDIT :

Try this one code

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string s = "";

        foreach (var item in listBox1.SelectedItems)
        {
            s += item.ToString() + ",";
        }

        textBox1.Text = s;
    }
Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28