question

MarkIsmail-5186 avatar image
0 Votes"
MarkIsmail-5186 asked MarkIsmail-5186 edited

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


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

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered MarkIsmail-5186 edited

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You are the man. Thank you so much.

0 Votes 0 ·