Search code examples
c#listviewcompact-framework

How to obtain the top most visible item in a ListView using Compact Framework?


How can I obtain the index of the top visible item in a C# ListView using the .NET Compact Framework version 3.5? The Compact Framework does not have the TopItem property in the ListView class.

My goal is to retrieve the index, refresh my ListView that contains data from a database, and return to that item after the refresh is completed. I can us the EnsureVisible(int index) method to automatically scroll, but I need the index first.


Solution

  • I have no explanation as to why there isn't a CF property for this, since the underlying Win32 control supports it. You must call SendMessage with the LVM_GETTOPINDEX constant. Something along these lines:

    private const int LVM_GETTOPINDEX = 0x1027;
    
    [DllImport("coredll.dll", SetLastError = true)]
    static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
    
    public static int GetTopIndex(this ListView lv)
    {
        return SendMessage(lv.Handle, LVM_GETTOPINDEX, 0, 0);
    }