How to scroll by one using mouse wheel instead of the default value ' 3 ' in Listview VB.net

Mark Ismail 21 Reputation points
2021-04-07T15:01:59.26+00:00

I have a modified list view (VB.net). When I scroll using the mouse wheel. It scrolls by 3 rows. I want it to scroll by one row at time using mouse wheel. Any help would be appreciate it.

Public Class listviewEx
Inherits ListView

Private Declare Function ShowScrollBar Lib "user32" (ByVal hwnd As IntPtr, ByVal wBar As Integer,
ByVal bShow As Boolean) As Integer
' Constants
Private Const SB_HORZ As Integer = 0

Private Const WM_HSCROLL As Integer = &H114
Private Const WM_VSCROLL As Integer = &H115

Public Event Scroll As ScrollEventHandler

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
ShowScrollBar(MyBase.Handle, SB_HORZ, False)
If m.Msg = &H115 Then
' Trap WM_VSCROLL
End If
End Sub

Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.EnableNotifyMessage, True)
End Sub

End Class

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,570 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2021-04-07T18:10:56.397+00:00

    Check an approach:

    Public Class listviewEx
        Inherits ListView
    
        Protected Overrides Sub WndProc(ByRef m As Message)
    
            Const WM_MOUSEWHEEL = &H20A
    
            Select Case m.Msg
                Case WM_MOUSEWHEEL
                    If TopItem IsNot Nothing Then
                        Dim d As Int16 = m.WParam.ToInt32 >> 16
                        Dim i As Integer
    
                        If d > 0 Then
                            i = Math.Max(TopItem.Index - 1, 0)
                        Else
                            i = Math.Min(TopItem.Index + 1, Items.Count - 1)
                        End If
    
                        TopItem = Items(i)
                    End If
    
                    m.Result = IntPtr.Zero
    
                    Return
    
            End Select
    
            MyBase.WndProc(m)
    
        End Sub
    
    End Class
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful