Reputation: 13
Is there a way to copy the text of the selected subitem from a ListView in .NET Winforms?
Upvotes: 1
Views: 1986
Reputation: 707
The following code snippet will give you the sub item value and sub item column name (if you store the col name in the tag value of the sub item when you create it.
Point workItemsListViewLastHit;
private void workItemsListView_MouseUp(object sender, MouseEventArgs e)
{
workItemsListViewLastHit = e.Location;
}
private void workItemsListView_DoubleClick(object sender, EventArgs e)
{
ListViewHitTestInfo HTI = workItemsListView.HitTest(workItemsListViewLastHit);
if (HTI.Item != null)
{
string field = HTI.SubItem.Tag as string;
string value = HTI.SubItem.Text;
}
}
Upvotes: 0
Reputation: 941455
Unless I'm really missing something fundamental, you can't select a subitem. You can select the subitem in the first column or you can set the FullRowSelect property to True. Neither helps you to determine what subitem the user might be interested in, there's no way to guess what to copy to the clipboard.
Use DataGridView to work around that.
Upvotes: 0
Reputation: 3149
Each item inside of a ListView
control is represented by a ListViewItem
. The ListViewItem
has a property called SubItems
which starts from the very first column of data in the ListView
control.
To copy data from a column, get the selected ListViewItem
and reference the Text
property available from the SubItems
property.
For example,
int theSelectedIndex = 0; // this should be the index of your selected item in the list
int theSubItemIndex = 0; // this should be the index of the subitem whose text you want to copy
ListViewItem lvItem = listView1.SelectedItems[theSelectedIndex];
string text = lvItem.SubItems[theSubItemIndex].Text;
Upvotes: 2