2011-05-16 45 views
3

如何知道WinForms ListView滾動條何時到達底部? 發生這種情況時,我希望listview中填充更多的數據(在我的情況下,這在理論上是無止境的)。檢測ListView滾動條何時到達WinForms底部

OnScroll事件給我從頂部的滾動值,但我無法知道用戶是否可以滾動任何更多或沒有。

回答

3

我發現使用一些代碼從大ObjectListView代碼項目的答案: http://www.codeproject.com/KB/list/ObjectListView.aspx

調用GetScrollInfo:

private const int SIF_RANGE = 0x0001; 
    private const int SIF_PAGE = 0x0002; 
    private const int SIF_POS = 0x0004; 
    private const int SIF_DISABLENOSCROLL = 0x0008; 
    private const int SIF_TRACKPOS = 0x0010; 
    private const int SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS);   
    private const int SB_HORZ = 0; 
    private const int SB_VERT = 1; 

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
    public static extern bool GetScrollInfo(IntPtr hWnd, int fnBar, SCROLLINFO scrollInfo); 

    public static SCROLLINFO GetFullScrollInfo(ListView lv, bool horizontalBar) { 
     int fnBar = (horizontalBar ? SB_HORZ : SB_VERT); 

     SCROLLINFO scrollInfo = new SCROLLINFO(); 
     scrollInfo.fMask = SIF_ALL; 
     if (GetScrollInfo(lv.Handle, fnBar, scrollInfo)) 
     return scrollInfo; 
     else 
     return null; 
    } 

與此數據結構:

[StructLayout(LayoutKind.Sequential)] 
    public class SCROLLINFO 
    { 
     public int cbSize = Marshal.SizeOf(typeof(SCROLLINFO)); 
     public int fMask; 
     public int nMin; 
     public int nMax; 
     public int nPage; 
     public int nPos; 
     public int nTrackPos; 
    } 

nMax給出總的最大滾動值,包括滾動手柄本身,所以實際有用的最大值是nMax-nPage,其中nPage是滾動手柄的大小。

這很好用!

0

我無法直接回答你的問題,但從你的描述中,聽起來你真的想用列表視圖的虛擬模式來管理大型數據集。

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode.aspx

+0

在這種情況下,虛擬模式可能很聰明,但它不能解決我的問題。 我的數據是無限的(它是日期的列表視圖),所以我不能在2100(?)之前顯示數百萬行的巨大列表視圖。 當用戶到達底部時,我需要列表增長。 – adams 2011-05-16 19:47:29

+0

@gpgemini嗯,好的。 – 2011-05-16 20:40:21