RobinPerris / DarkUI

Dark themed control and docking library for .NET WinForms.
MIT License
837 stars 186 forks source link

[Feature] List view index from point #53

Open Hi-ImKyle opened 3 years ago

Hi-ImKyle commented 3 years ago

As it currently sits the dark list view control has no IndexFromPoint method, I feel as though it would be a good addition and so I'm kindly asking for it to be implemented. I would do it myself but I am not confident enough with something like that.

Thanks.

Dutchs commented 2 years ago

It's basically what DarkListView.OnMouseDown() does, minus selection:

public int IndexFromPoint(Point p)
{
    int result = -1;

    if (Items.Count != 0)
    {
        var pos = new Point(p.X + Viewport.Left, p.Y + Viewport.Top);

        var range = ItemIndexesInView().ToList();

        var top = range.Min();
        var bottom = range.Max();
        var width = Math.Max(ContentSize.Width, Viewport.Width);

        for (var i = top; i <= bottom; i++)
        {
            var rect = new Rectangle(0, i * ItemHeight, width, ItemHeight);

            if (rect.Contains(pos))
            {
                result = i;
                break;
            }
        }
    }

    return result;
}