topofsteel topofsteel - 1 year ago 95
C# Question

Get single listView SelectedItem

I have the

property of the listView set to false and I'm trying to get a single listViewItem. But the available property is
. I've been using the following code...

foreach (ListViewItem item in listView1.SelectedItems)
//do something with item.text or whatever

Because I know there will only be one item selected. What is the correct way of doing this?

Answer Source

Ususally SelectedItems returns either a collection, an array or an IQueryable.

Either way you can access items via Index as with an array:

String text = listView1.SelectedItems[0].Text; 
//do something

By the way you can always save an item you want to look at into a var lookat = whatever you wantand check its structure in Locals after setting a breakpoint

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download