Search code examples
c#.netlistviewitem

accessing ListViewItem.Bounds or ListViewItem.GetBounds(..) throws Exception


I get an Exception at lvi.Bounds But I don't know what i can check to stop it. It seems to only happen when I am displaying the control and call this method on my Resize event, or sizeChanged, or after I have created everything, but before my code returns to Windows.

private List<uint> GetIndexes(ListView vv)
    {
        List<uint> ret = new List<uint>();
        foreach (ListViewItem lvi in vv.Items)
        {
            uint uiKey = ((uint)lvi.Tag);
            if (!requestedImgs.Contains(uiKey) && lvi.Bounds.IntersectsWith(vv.ClientRectangle) && DctImages.Contains(uiKey))
            {
                ret.Add(uiKey);
            }
        }
        return ret;
    }

Solution

  • The reason i have found for this is that I was calling

    itemPanel1.Sorting = SortOrder.Ascending;
    itemPanel1.Sort();
    

    after I had added the items, and calling

    itemPanel1.Select();
    

    before I then went through all my items and checked the bounds, which caused the exception above.

    So i don't understand it, but have found a fix.