Alex
Alex

Reputation: 4938

Copy Listview Cell

Is it possible to copy a selected listview cell? I know it's possible to do that in a datagridview but I'm not sure if we can in a listview... For Example:

Copy red rectangle

Dim s As String = ""

For Each lsvrow As ListViewItem In lwBills.SelectedItems
    s &= lsvrow.Text & ControlChars.NewLine
Next

Clipboard.SetDataObject(s)

This will copy the row's text (10133 in this example). The subitems include 206-0015-04B and K-3390 but my current example will not copy the subitems. I'm not looking to copy the whole row though, only the part where I have right clicked (in this case 206-0015-04B)

Can a Listview do that?

Upvotes: 0

Views: 1973

Answers (1)

Martin
Martin

Reputation: 16433

This can be achieved using a combination of the MouseClick event and the HitTest method of the ListView.

Handle the MouseClick event, and then during the event handler use the HitTest method to see which SubItem is under the mouse pointer, as follows:

Private Sub lwBills_Click(sender As System.Object, e As MouseEventArgs) Handles lwBills.MouseClick
    Dim Info As ListViewHitTestInfo
    Dim s As String

    Info = lwBills.HitTest(e.Location)
    s = Info.SubItem.Text
    Clipboard.SetDataObject(s)
End Sub

I am assuming that lwBills is the name of your ListView in the above example.

Upvotes: 2

Related Questions