ListView GetItemAt (e.X, e.Y) method

M

Michael C

Hi all,

When I try to use the GetItemAt (e.X, e.Y) method in a ListView MouseDown()
event, it only works some of the time. If I right-click on an open area of
the ListView, I get null back as expected. If I right-click on a
ListViewItem, I get that ListViewItem back. But then... if I right-click on
an open area of the ListView again I get the last ListViewItem I clicked on
back... I'm sure there's a way around this. GetItemAt doesn't seem to be
holding onto the last value returned. I did something similar with the
TreeView GetNodeAt method, and it works as advertised. Anyone have any
ideas on this?

Thanks in advance,
Michael C.
 
S

Shakir Hussain

Michael,

I normally do this way, which is pretty accurate in retrieving items. You
can use this code, on mousehover, or mousemove, or mouse down event.

//use cursor points.
Point p = lstViewTest.PointToClient(new Point (Cursor.Position.X
,Cursor.Position.Y));

//now get the item.
ListViewItem pItem = lstViewTest.GetItemAt(p.X,p.Y )

Shak
 
M

Michael C

Thanks Shakir,

It looks like my bug was caused by a context menu I was using, but wasn't
clearing out between pop-ups. I used your code with the Clear() method of
the pop-up event for the context menu and it works great now!

Thanks,
Michael C.
 

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