Get the SubItem value by double-clicking on an item in a ListView

I have a two-column list, and when I double-click on an element, I need to display the value of its corresponding element in the TextBox control. How can i do this?

I searched Google, but it didn’t return anything, perhaps because I’m not quite sure what to look for.

thanks

+4
source share
1 answer

The MSDN links you want to read are ListViewItem and ListViewSubItem .
You access the elements of the list item list item through the ListViewItem.SubItems property. The most important thing to remember is that the first subitem refers to the owner list item, so to access the actual subitems you need to index from 1. This will return you a ListViewSubItem , and you can get its text enter ListViewSubItem.Text .

i.e
SubItems[0] provides a parent list view item
SubItems[1] provides the first subheading, etc.

Fast, nasty piece of code

 private void listView1_SelectedIndexChanged(object sender, EventArgs e) { ListView.SelectedIndexCollection sel = listView1.SelectedIndices; if (sel.Count == 1) { ListViewItem selItem = listView1.Items[sel[0]]; textBox1.Text = selItem.SubItems[1].Text; } } 

Hope that helps

+7
source

Source: https://habr.com/ru/post/1310644/


All Articles