ListView only recognizes Items, how about SubItems?

G

Guest

Hi. I use a ListView to display data in tabular form.

Each ListView row corresponds to a data record.
The ListView Item of the record is the record key or code.
Each SubItem in that row represents a field from the record.

I implemented a context menu so the user can "Copy" the cell content to the
clipboard with this code:


Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
'Displays a context menu with the option to "Copy" to the clipboard.

'Activate context menu if user right-clicked.
If e.Button = MouseButtons.Right Then
Dim ClickPoint As Point = New Point(e.X, e.Y)
Dim LVItem As ListViewItem = ListView1.GetItemAt(ClickPoint.X,
ClickPoint.Y)

'Copy data only if item contains some data.
If Not LVItem Is Nothing Then
'Code to copy to ciipboard...
End If
End If
End Sub

The problem is that GetItemAt() returns Nothing if the click happened on a
SubItem, that is, any column but the leftmost.
How can I allow the users to copy the data from the other columns to the
clipboard?

Thanks in advance

Richard
 
I

Imran Koradia

I've seen people having problems with this. In theory, you should get the
ListView Item even when you click on the subitems (as per documentation of
the GetItemAt method). You can try one of these:

1. Try putting your code in the mouseup event instead of the mouse down
event.

2. If you absolutely want it in the mousedown event, you could do something
like this:

Dim p As Point = ListView1.PointToClient( _
New Point(Cursor.Position.X, Cursor.Position.Y))

Dim LVItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y)

hope that helps..
Imran.
 
G

Guest

Thank you Imran, but this is not a viable solution.
It seems that ListView1.GetItemAt() method only returns something when
clicked on the Item (the 1st column of the ListView). When a subitem (any
other column) is clicked, It returns Nothing.

Thank you,

Richard
 
L

Lloyd Sheen

There is a property (don't have VS where I am) that will determine when the
GUI will respond to a mouse click to select an item. If you don't set this
the listview item will only be selected if you click the first column. I
would bet that the GetItemAt will respond in the same manner. Set this
property to true and I think you will get what you are looking for.

Lloyd Sheen
 
L

Lloyd Sheen

Property is FullRowSelect

Lloyd Sheen

Lloyd Sheen said:
There is a property (don't have VS where I am) that will determine when
the GUI will respond to a mouse click to select an item. If you don't set
this the listview item will only be selected if you click the first
column. I would bet that the GetItemAt will respond in the same manner.
Set this property to true and I think you will get what you are looking
for.

Lloyd Sheen
 
I

Imran Koradia

Richard,

I believe Lloyd's answer should solve your problem. I looked in my code and
I've been using the same code you posted without any problems. I guess I
overlooked the fact that FullRowSelect was True in my case - I just assumed
you would have that set to True since that's how we are accustomed to using
listviews. You should set this property to True (from the designer or at
runtime - whichever) which will ensure that the entire row is highlighted
and then you should be able to get the listview item by clicking anywhere on
that row.

Imran.
 
G

Guest

Thank you Lloyd, you are right, with FullRowSelect = True the Item becomes
the whole line and it recognizes the click. In order to find the subitem that
was clicked, I suppose I will have to calculate the width of each column in
pixels. Is that correct?

Thank you

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top