locked
get the text of an single listviewitem

    Question

  • Hi,I would like to get the text of an item of my listview. I would like to use the indices of the listview to select the item.

    So my targeted code would be something like this: string a = listview.items[3].Text;

    Sadly it doesnt work as simple as that. Using an listviewitem to read the text out, i had not more luck ether. 

    Right now im not sure if it is possilble to read out the text propverty of the item. Do i need to change to anther type of list?A work-arround would be rly nice to know...

    I would appreciate every help:)

    Panda

    Monday, May 5, 2014 1:41 PM

Answers

  • What sort of items are you adding?

    You should get the same item back that you added. If you call listview.add("foo"); then you'll get the string "foo" back from listview.Items[x];

    How you extract the text of the item will depend on the item. If it's an object with a Text property then you can use that property. Otherwise you'll need to use whichever property is appropriate for the item. Calling its ToString may or may not help.

    A container like a ListViewItem doesn't generally have any knowledge of text and so you won't be able to get it directly from that. You may be able to find it in child objects, but it will be much easier to find from a data object that directly provides display text rather than from the GUI template that displays it.

    --Rob

    • Marked as answer by Pyropanda94 Tuesday, May 6, 2014 7:35 AM
    Monday, May 5, 2014 4:10 PM
    Owner

All replies

  • What isn't working? Does the item you are bound to have a "Text" property? In what way does it not work? What results or error do you get?

    Listview.Items contains the bound objects themselves, not the datatemplate which displays them. You will need to cast this from object to its real type so you can call its properties.

    --Rob

    Monday, May 5, 2014 1:56 PM
    Owner
  • I added the items into the listview viva listview.add. I have no objects bound to the listview( or what is it doing when im using the .add ? listviewitems i guess). Thats why i have to get the itemtext from the listview itself.

    If this is not possible i guess i have to work with data binding...

    Monday, May 5, 2014 3:19 PM
  • What sort of items are you adding?

    You should get the same item back that you added. If you call listview.add("foo"); then you'll get the string "foo" back from listview.Items[x];

    How you extract the text of the item will depend on the item. If it's an object with a Text property then you can use that property. Otherwise you'll need to use whichever property is appropriate for the item. Calling its ToString may or may not help.

    A container like a ListViewItem doesn't generally have any knowledge of text and so you won't be able to get it directly from that. You may be able to find it in child objects, but it will be much easier to find from a data object that directly provides display text rather than from the GUI template that displays it.

    --Rob

    • Marked as answer by Pyropanda94 Tuesday, May 6, 2014 7:35 AM
    Monday, May 5, 2014 4:10 PM
    Owner
  • Thanks that helped in fact alot. The listview was mostly filled with strings. But i added stuff like phonenumbers too, that I wanted to use as string. The listview.Items[x] returned the number as integer. So i just typecasted it and im fine now.Well i guess the Visual Studio Error Feedback confused me a lot, because i thought of the listview of a list of strings.

    Simple Misstake. A lot of wasted time. And a little thing learn today.

    Thanks for the support :)

    Panda

    Monday, May 5, 2014 4:53 PM