C#: How do you check if the mouse click position was only inside an item in Column[0] in a listview under virtualmode?
C#:如何检查鼠标单击位置是否仅位于virtualmode下列表视图中列[0]中的项目内?
I can get the the Item object the mouse just clicked using ListView.GetItemAt, but after that, how could I check if it was clicked within column[0]?
我可以使用ListView.GetItemAt获取鼠标刚刚单击的Item对象,但在那之后,我如何检查它是否在列[0]中被单击?
3 个解决方案
#1
Disregard, I found a solution after tinkering with the code some more. Here's the solution I used:
无视,我在修改代码之后找到了一个解决方案。这是我使用的解决方案:
private void lvListView_MouseClick(object sender, MouseEventArgs e)
{
ListView lv = (ListView)sender;
ListViewItem lvi;
if (e.X > lv.Columns[0].Width)
{
lvi = null;
}
else
{
lvi = lv.GetItemAt(e.X, e.Y);
}
if (lvi != null)
{
lvi.Checked = !lvi.Checked;
lv.Invalidate(lvi.Bounds);
}
}
#2
Something like this perhaps (inside a mouse event: e is of type MouseEventArgs):
也许这样的东西(在鼠标事件中:e是MouseEventArgs类型):
// get the rectangle for the first item; used for getting sideways scrolling offset
Rectangle r = listView1.GetItemRect(0);
int leftOffset = r.Left;
if (listView1.Columns[0].Width + leftOffset > e.X)
{
// first column
}
else
{
// other column
}
Update: missed that it was only the first column that was interesting; first solution picked out the column index under the mouse; this picks only the "first" or "other" cases. Note that it also takes sideways scrolling into consideration.
更新:错过了它只是第一个有趣的专栏;第一个解决方案在鼠标下挑出了列索引;这只选择了“第一”或“其他”案例。请注意,它还需要横向滚动。
#3
ListViewItem has a GetSubItemAt member that would probably help.
ListViewItem有一个可能有用的GetSubItemAt成员。
#1
Disregard, I found a solution after tinkering with the code some more. Here's the solution I used:
无视,我在修改代码之后找到了一个解决方案。这是我使用的解决方案:
private void lvListView_MouseClick(object sender, MouseEventArgs e)
{
ListView lv = (ListView)sender;
ListViewItem lvi;
if (e.X > lv.Columns[0].Width)
{
lvi = null;
}
else
{
lvi = lv.GetItemAt(e.X, e.Y);
}
if (lvi != null)
{
lvi.Checked = !lvi.Checked;
lv.Invalidate(lvi.Bounds);
}
}
#2
Something like this perhaps (inside a mouse event: e is of type MouseEventArgs):
也许这样的东西(在鼠标事件中:e是MouseEventArgs类型):
// get the rectangle for the first item; used for getting sideways scrolling offset
Rectangle r = listView1.GetItemRect(0);
int leftOffset = r.Left;
if (listView1.Columns[0].Width + leftOffset > e.X)
{
// first column
}
else
{
// other column
}
Update: missed that it was only the first column that was interesting; first solution picked out the column index under the mouse; this picks only the "first" or "other" cases. Note that it also takes sideways scrolling into consideration.
更新:错过了它只是第一个有趣的专栏;第一个解决方案在鼠标下挑出了列索引;这只选择了“第一”或“其他”案例。请注意,它还需要横向滚动。
#3
ListViewItem has a GetSubItemAt member that would probably help.
ListViewItem有一个可能有用的GetSubItemAt成员。