How to ListView SubItem under mouse?

  • Thread starter Tom Sage via .NET 247
  • Start date
T

Tom Sage via .NET 247

During the MouseHover event the Item in ListView is easy to get using GetItemsAt(X,Y). How does one obtain the text in a SubItem cell? There is no GetSubItemAt method.

Thanks for any ideas,
 
M

Morten Wennevik

Hi Tom,

Assuming you use MouseMove to get the X and Y, you can calculate which sub item is under the cursor by comparing X with the column widths.

Note, this will only work if FullRowSelect = true; or MouseMove won't trigger the event.

Some sample code from the MouseMove event, but you can easily store the position for later use by MouseHover

ListViewItem l = listView1.GetItemAt(e.X, e.Y);
if(e.X <= listView1.Columns[0].Width)
label1.Text = l.SubItems[0].Text;
else if(e.X <= (listView1.Columns[1].Width + listView1.Columns[0].Width))
label1.Text = l.SubItems[1].Text;
else if(e.X <= (listView1.Columns[2].Width + listView1.Columns[1].Width + listView1.Columns[0].Width))
label1.Text = l.SubItems[2].Text;


Happy coding!
Morten Wennevik [C# MVP]
 

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