How can I determine if a VB.NET ListView is displaying vertical scrollbar to user

后端 未结 2 866
你的背包
你的背包 2021-01-21 13:17

I feel like this should be something simple, but I can\'t seem to find out how to do this.

I have a list view control, and I would just like to be able to determine if t

相关标签:
2条回答
  • 2021-01-21 14:06

    Try this code, all .net, and only a 1 line function.

    Private Function IsVerticalScrollVisible(ByVal lst As ListView) As Boolean
        If lst.Items.Count > 0 Then _
            If (lst.Items(0).Bounds.Top - lst.TopItem.Bounds.Top < 0) Or _
            (lst.Items(lst.Items.Count - 1).Bounds.Bottom > lst.ClientSize.Height) _
            Then Return True
    End Function
    
    0 讨论(0)
  • 2021-01-21 14:16

    This is a NET update of the MSDN answer (if you look, that is VB6 related):

    'Pinvokes - these are usually Shared methods in a 
    ' Win32NativeMethods class you accumulate 
    Private Const GWL_STYLE As Integer = -16
    Private Const WS_HSCROLL = &H100000
    Private Const WS_VSCROLL = &H200000
    
    <DllImport("user32.dll", SetLastError:=True)> _
    private Shared Function GetWindowLong(ByVal hWnd As IntPtr, 
                           ByVal nIndex As Integer) As Integer
    End Function
    
    ' sometimes you use wrappers since many, many, many things could call
    ' SendMessage and so that your code doesnt need to know all the MSG params
    Friend Shared Function IsVScrollVisible(ByVal ctl As Control) As Boolean
        Dim wndStyle As Integer = GetWindowLong(ctl.Handle, GWL_STYLE)
        Return ((wndStyle And WS_VSCROLL) <> 0)
    
    End Function
    
    ' to be complete:
    Friend Shared Function IsHScrollVisible(ByVal ctl As Control) As Boolean
        Dim wndStyle As Integer = GetWindowLong(ctl.Handle, GWL_STYLE)
        Return ((wndStyle And WS_HSCROLL) <> 0)
    
    End Function
    

    Elsewhere, subscribe to the ClientSizeChanged event:

    Private VScrollVis As Boolean = False
    Private Sub lv_ClientSizeChanged(sender As Object, e As EventArgs) 
                   Handles myListView.ClientSizeChanged
    
        VScrollVis = IsVScrollVisible(Me)
    
        MyBase.OnClientSizeChanged(e)
    End Sub
    

    You did not indicate what you wanted to do about it. You could raise a new event whenever VScrollVis changes or you can write code to "fix" the control if the HScroll shows up simply because the VScroll is now Visible.


    I just want to to call a function and have it return true if the scrollbar is visible

    ' expose PInvoke if needed, convert to non-Shared
    Public Function IsVerticalScrollVisible(ctl As Control)
       Return IsVScrollVisible(ctl)
    End Function
    
    Public Function IsHorizontalScrollVisible(ctl As Control)
       Return IsHScrollVisible(ctl)
    End Function
    
    0 讨论(0)
提交回复
热议问题