Ryan Krage Ryan Krage - 1 year ago 103
C# Question

Get the position of a ListView item that was just created?

In my winform I create an item in a listview like so;

ListViewItem item = listView1.Items.Add("an item");

How can I get its position index? I've tried the following;

int position = item.Position;
int position = item.SelectedIndices[0];

But neither works.

I would use
but the newly created item isn't selected so that won't work either.

EDIT: By position, I meant the index of the item, not its co-ordinates. Apologies for the confusion!

Answer Source

The Add methods all return a ListViewItem object. The ListViewItem object has an Index property that will return the zero-based index of the item within the ListView control.

The Position method does something else entirely. It returns the coordinates of the ListViewItem's top-left corner. These coordinates are relative to the ListView control.

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