ListView: How can I tell which subitem is clicked?

P

Peter Steele

I have a form containing a ListView with four columns, creating a multi-cell
table. I want to have an event which can determine which cell in the table
is under the cursor when a double-click is done. The double-click event of
the ListView double-click event handler only passes in the sender object and
the EventArgs object. How can I figure out the specific cell where the click
occurred?

Peter Steele
CIENA Corp.
 
P

Peter Steele

Okay, so I decided to try something with the MouseDown event and do this:

ListViewItem cell = ListView1.GetItemAt(e.X, e.Y);

This gives me a reference to the correct row of the table but not the
subitem. In other words, this always gives me the cell at column 0. I want
to fine tune this so that I know the subitem the user has clicked on, which
could be the cell at any given column along that row. Is there a way to do
this?
 
C

Christoph Nahr

Okay, so I decided to try something with the MouseDown event and do this:

ListViewItem cell = ListView1.GetItemAt(e.X, e.Y);

A general note: For determining that someone's clicked on your
ListView you can simply use the SelectedIndexChanged (single click)
and ItemActivate (double click, configurable) events. You can get the
current row by examining the first element of
SelectedIndices/SelectedItems inside the event handler.

As for figuring out the column, that's something you'll have to do
yourself. Get the Columns collection, then the Width of each
ColumnHeader, then start adding up to see which point your mouse
cursor is at (the static property Cursor.Position returns the current
mouse cursor coordinates).

Note that Cursor.Position may be slightly changed from the click point
at the time you evaluate it so you might want to stick with the
MouseDown event for this particular purpose after all.
 

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