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

后端 未结 2 869
你的背包
你的背包 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: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
    
     _
    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
    

提交回复
热议问题