0

I have an Object and I want to have a List of that Object in a Control List such as ListView.

When I DoubleClick that Object in ListView, I would like to get that specific Object and use it on another Form. But I can't retrieve that Object when I use listview1.SelectedItems.

How can I achieve that ?

2
  • If you're setting the View to Details then you probably shouldn't be using a ListView in the first place. If you have a list of objects in the first place then you probably ought to be binding that to a BindingSource and binding that to a DataGridView. Each row in the grid has a DataBoundItem property that will automatically expose the item from the underlying list. Commented May 2, 2020 at 12:24
  • By the way, the Visual Studio tag specifically states that it is for issues using the IDE, not for problems with code written in VS. Only use relevant tags. Commented May 2, 2020 at 12:25

1 Answer 1

2

A ListView contains ListViewItem objects. That's it, that's all. When the user selects an item, they are selecting a ListViewItem object. If you want to then access some other object that is related to that item, it's up to you to create an explicit relationship of some kind between them. A common means to do that is to assign those original objects to the Tag property of the corresponding ListViewItem, e.g.

For each something As Thing In myThings
    Dim item As New ListViewItem

    item.Tag = something

    '...

    myListView.Items.Add(item)
Next

You can then get those objects back from the Tag properties of the selected items, e.g.

For Each item As ListViewItem In myListView.SelectedItems
    Dim something = DirectCast(item.Tag, Thing)

    '...
Next
Sign up to request clarification or add additional context in comments.

4 Comments

Does the Tag property can access to the specific Object in a collection ?
Edit: I first add the Object in a collection then I add to ListView. Because, what I understand about your code is, it's assigned a Tag to each Object and you add that Item in the ListView . But the Tag property assign automatically a Tag ?
The Tag property is just a general-purpose holder where you can put any object you like that is associated with the ListViewItem. You said that you want to be able to access a specific object when you select an item and I told you how to do. Do it. Put that object in the item's Tag property. It's not rocket surgery.
Yes I made it worked. I thought that I have to put my own Tag that's why I was confused. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.