I have done this and it works fine :
private int getcolumn()
{
Point mousePosition = base.PointToClient(Control.MousePosition);
ListViewHitTestInfo hit = base.HitTest(mousePosition);
return hit.Item.SubItems.IndexOf(hit.SubItem);
}
But works if in the listview there is at least one item. My list is empty, so how can i get column index? Thanks in advance.
private int getcolumn()
{
Point mousePosition = base.PointToClient(Control.MousePosition);
ListViewHitTestInfo hit = base.HitTest(mousePosition);
if Item.SubItems.Any()
{
return hit.Item.SubItems.IndexOf(hit.SubItem);
}
else
{
return -1;
}
}
An empty list is going to return a null subitem.
However, while the example below is not very elegant, you can see how to use the Y position to determine which column the Y is associated with:
private int getcolumn()
{
Point mousePosition = base.PointToClient(Control.MousePosition);
switch(mousePosition.Y)
{
case mousePosition.Y >= 0 && mousePosition.Y <= base.Columns(0).Width:
Return base.Columns(0).index
break;
case mousePosition.Y >= base.Columns(0).Width && mousePosition.Y <= base.Columns(1).Width:
Return base.Columns(1).index
break;
case mousePosition.Y >= base.Columns(0).Width + base.Columns(1).Width && mousePosition.Y <= base.Columns(2).Width:
Return base.Columns(2).index
break;
default:
Return -1
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.