Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone.
I have defined the following ListView with the View property in Tile mode, since it is assigned a list of images, ImageList1:

C#
ListView1.AllowDrop = true;
ListView1.View = View.Tile;
ListView1.FullRowSelect = true;
ListView1.GridLines = true;
ListView1.Columns.Add("indice", -2, HorizontalAlignment.Right);
ListView1.Columns.Add("nombre", -2, HorizontalAlignment.Left);
ListViewCanalesDelFichero.LargeImageList = ImageList1;


When I access the ListView.TopItem property to get the item that visually appears first in the ListView as follows,
C#
string item = ListView1.TopItem.SubItems[1].Text;

an exception is thrown:
"You can't get the top item in the LargeIcon, SmallIcon, or Tile view. "
The problem is in the View property that is in Tile. In View.Details mode there is no problem.
What do I have to do to, while maintaining the Tile mode, get the first visible item?

What I have tried:

The problem is in the View property that is in Tile.  In View.Details mode there is no problem.
Posted
Updated 15-Jul-22 21:43pm

Look at the error message; it clearly states that you can only do this in Details view, as referred in the documentation at ListView.TopItem Property (System.Windows.Forms) | Microsoft Docs[^].
 
Share this answer
 
This works for me

int FindFirstVisibleItem(Rectangle rectangle)
{
    foreach (ListViewItem item in Items)
    {
        if (item == null) continue;
        if (rectangle.IntersectsWith(item.Bounds))
        {                   
            Invalidate();
            return item.Index;
            }
        }
        return -1;
}


and call it as in as follows

int firstVisibleItem = FindFirstVisibleItem(new Rectangle(0, 0, e.Bounds.Width, e.Bounds.Height));
if (e.ItemIndex == firstVisibleItem)
    e.Graphics.DrawRectangle(Pens.Red, e.Bounds);
 
Share this answer
 
v2
Comments
CHill60 20-May-24 5:17am    
This works for you? When the View is Tile View? I doubt it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900