使用ListView.GetItemAt 方法 把鼠标的坐标转换后传到这个方法做为参数获取鼠标坐标下的Item。public ListViewItem GetItemAt ( int x, int y ) private void ListView1_MouseDown(object sender, MouseEventArgs e) { ListViewItem selection = ListView1.GetItemAt(e.X, e.Y); // If the user selects an item in the ListView, display // the image in the PictureBox. if (selection != null) {
} }
我描述得有点问题,能上个图就好了,假设下面是个listviewitem sub sub sub a 1 1 1 b 2 2 2 c 3 3 3我点其中某个subitem,想得到它的位置,该怎么办呢? sublist的位置应该可以用listView1.Items[index].GetSubItemAt(e.X, e.Y)确定 但是我得不到index的值
可以使用IndexOf啊。参考: private void listView1_MouseDown(object sender, MouseEventArgs e) { // Get the item at the mouse pointer. ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y); ListViewItem.ListViewSubItem subItem = null; // Get the subitem 120 pixels to the right. if (info != null && info.Item != null) { subItem = info.Item.GetSubItemAt(e.X, e.Y); int index = listView1.Items.IndexOf(info.Item); } }
listView1.Items.IndexOf(item);
int x,
int y
)
private void ListView1_MouseDown(object sender, MouseEventArgs e)
{ ListViewItem selection = ListView1.GetItemAt(e.X, e.Y); // If the user selects an item in the ListView, display
// the image in the PictureBox.
if (selection != null)
{
}
}
a 1 1 1
b 2 2 2
c 3 3 3我点其中某个subitem,想得到它的位置,该怎么办呢?
sublist的位置应该可以用listView1.Items[index].GetSubItemAt(e.X, e.Y)确定
但是我得不到index的值
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
// Get the item at the mouse pointer.
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y); ListViewItem.ListViewSubItem subItem = null; // Get the subitem 120 pixels to the right.
if (info != null && info.Item != null)
{
subItem = info.Item.GetSubItemAt(e.X, e.Y);
int index = listView1.Items.IndexOf(info.Item);
}
}