Public Class ListViewEx
Inherits System.Windows.Forms.ListView
' Constants passed to the Scroll event
Public Enum ScrollDirection
Horizontal = 0
Vertical
End Enum
' Window messages we need to intercept
Private Enum WindowMessages As Integer
WM_HSCROLL = &H114
WM_NOTIFY = &H4E
WM_VSCROLL = &H115
End Enum
' Notification messages for WM_NOTIFY
Private Enum HeaderNotifications As Integer
HDN_FIRST = -300&
HDN_BEGINTRACK = (HDN_FIRST - 26) ' Begin Column resize
HDN_ENDTRACK = (HDN_FIRST - 27) ' End Column resize
HDN_DIVIDERDBLCLICK = (HDN_FIRST - 25) ' Double-click on divider
HDN_ITEMCHANGING = (HDN_FIRST - 20) ' Column resizing
End Enum
' Structure to hold extra information for WM_NOTIFY messages
<StructLayout(LayoutKind.Sequential)> Private Structure NMHDR
Friend hWndFrom As Integer
Friend idfrom As Integer
Friend code As Integer
End Structure
Protected mblnAllowColumnResize As Boolean = True
Public Event ColumnsResized(ByVal sender As Object)
Public Event Scroll(ByVal sender As Object, _
ByVal Direction As ScrollDirection)
'***********************************************************************
' Public Sub New()
' Description: Class constructor
'***********************************************************************
Public Sub New()
MyBase.New()
End Sub
'***********************************************************************
' Protected Overridable Sub OnColumnHeaderResized()
' Description: Performs actions and raises events when a ListView
' column is resized.
'***********************************************************************
Protected Overridable Sub OnColumnHeaderResized()
RaiseEvent ColumnsResized(Me)
End Sub
'***********************************************************************
' Protected Overridable Sub OnScroll(ByVal Direction As
ScrollDirection)
' Description: Performs actions and raises events when the ListView
is
' scrolled.
'***********************************************************************
Protected Overridable Sub OnScroll(ByVal Direction As
ScrollDirection)
RaiseEvent Scroll(Me, Direction)
End Sub
'***********************************************************************
' Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
' Description: Handles Windows messages
'***********************************************************************
Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
' Execute parent WndProc. While we're subclassing anyway, we'll
' encapsulate it in a Try block so that we can catch that annoying
' NullReferenceException that gets thrown occasionally during a
' LabelEdit. NOTE: This may only be necessary in Beta 2.
Try
MyBase.WndProc(m)
Catch
Exit Sub
End Try
Select Case m.Msg
' Catch scroll messages and fire the Scroll event
Case WindowMessages.WM_HSCROLL :
OnScroll(ScrollDirection.Horizontal)
Case WindowMessages.WM_VSCROLL :
OnScroll(ScrollDirection.Vertical)
' Information about Column resizing comes throught WM_NOTIFY
Case WindowMessages.WM_NOTIFY
' Read in the NMHDR structure from LParam
With CType(m.GetLParam(GetType(NMHDR)), NMHDR)
' See what kind of notification we're dealing with...
Select Case .code
' The user is starting to drag-resize -- abort if not
allowed
Case HeaderNotifications.HDN_BEGINTRACK
If mblnAllowColumnResize = False Then m.Result = New
IntPtr(1)
' A header is being resized -- fire the event, or abort
Case HeaderNotifications.HDN_ITEMCHANGING
Select Case mblnAllowColumnResize
Case True : OnColumnHeaderResized() ' Fire the event
Case False : m.Result = New IntPtr(1) ' Abort
End Select
End Select
End With
End Select
End Sub
'*********************************************************************
' Public Property AllowColumnResize() As Boolean
' Description: Returns/sets a value which determines whether
ListView
' columns can be resized, either manually or in code.
' NOTE: Setting this property to False prevents resizing Columns
using
' their Width property as well as manually.
'*********************************************************************
Public Property AllowColumnResize() As Boolean
Get
Return mblnAllowColumnResize
End Get
Set(ByVal Value As Boolean)
mblnAllowColumnResize = Value
End Set
End Property
End Class