Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding items to list view with text label and key value

Tags:

c#

listview

I'm trying to add items to a ListView control. I wish to add the items with a text value (which is shown) and a hidden key value that it has when it is selected.

I've tried the following code:

string flows_path = "C:\\temp\\Failed Electricity flows\\";
            List<ListViewItem> flows_loaded = new List<ListViewItem>();

            foreach (string s in Directory.GetFiles(flows_path, "*.rcv").Select(Path.GetFileName))
            {
                ListViewItem new_item = new ListViewItem(s, 1);
                ListViewItem.ad

                // Add the flow names to the list
                flows_loaded.Add(new_item);

            }

But it tells me that ListViewItem doesn't have an overload of (string, int) and it doesn't appear to have a 'value', 'text' or 'key' value that I can set.

ListViewItem("My Item") works, but I don't know how to implement a key for each item.

like image 444
Luke Avatar asked Oct 17 '25 14:10

Luke


1 Answers

You can store an additional value associated with a ListViewItem by storing it in the Tag property.

ListViewItem new_item = new ListViewItem(s);
new_item.Tag = my_key_value;

ETA: Please remember that the Tag property is of type object, so in some cases you may need to explicitly cast values to the proper type when retrieving the value.

like image 131
John Willemse Avatar answered Oct 20 '25 05:10

John Willemse