Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get single listView SelectedItem

I have the MultiSelect property of the listView set to false and I'm trying to get a single listViewItem. But the available property is SelectedItems. 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?

like image 751
topofsteel Avatar asked Feb 26 '13 14:02

topofsteel


People also ask

Which method is used to obtain the selected option from a ListView?

To get which item was selected, there is a method of the ListView called getItemAtPosition.

What is a ListView subitem?

The Windows Forms ListView control can display additional text, or subitems, for each item in the Details view. The first column displays the item text, for example an employee number. The second, third, and subsequent columns display the first, second, and subsequent associated subitems.


1 Answers

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

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

String text = listView1.SelectedItems[0].Text; 

By the way, you can save an item you want to look at into a variable, and check its structure in the locals after setting a breakpoint.

like image 155
Vogel612 Avatar answered Oct 13 '22 16:10

Vogel612