Adam Schneider
Adam Schneider

Reputation: 295

Getting the Index of an Added ListView.Item

So all I am trying to do is get the index of an Item Programmatically added to a ListView from a Database.

Example: Index = lvwNames.Items.Add(Player)

Player is a Class that uses Get/Set methods for ID, FirstName, and, LastName.

I have googled my heart out to no avail.

EDIT

So I had a GetInformation() procedure that populated my Player Class. So I was helped by a personal friend, I just needed to pass in my ListViewItem ByRef into there and I can grab the data I needed (SUCH A DUMMY!). Still having an issue with the Index. I would like to have the newly added item selected after it is added.

Upvotes: 2

Views: 793

Answers (1)

user3697824
user3697824

Reputation: 536

Getting the Index of an Added ListView.Item as per the title.

Method A: Look at the count

 lvwNames.Items.Add(Player.ToString())
 Dim ndx = lvwNames.Items.Count-1

Method B: Use an object

 Dim lvi As New ListViewItem(Player.ToString())
 lvwNames.Items.Add(lvi)
 Dim ndx = lvi.Index

However, Player is an Object and there is no matching Add overload, with Option Strict (which all good code should use). The best you can do is pass it as ToString() as shown.

The result will be something like WindowsApplication1.Player unless your class overrides ToString(). In no case will the ListView parse it to put the properties ("Get/Set methods") as sub items which sort of sounds like what you expect.

For that, a DatagridView would be a better choice using a List(Of Player). Each property could be mapped to its own column.

Upvotes: 2

Related Questions