views:

1268

answers:

3

C#: How do you check if the mouse click position was only inside an item in Column[0] in a listview under virtualmode?

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]?

A: 

Something like this perhaps (inside a mouse event: e is of type 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.

Fredrik Mörk
A: 

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);
        }
    }
A: 

ListViewItem has a GetSubItemAt member that would probably help.

NascarEd