AnasBakez
AnasBakez

Reputation: 1230

How to make scroll event for ListView

I want to make an event for the scroll in ListView.

I have found something that works, but it only fires the event when using the scrollbar. It does not respond to scrolling by mouse wheel or arrows.

private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;

public event EventHandler Scroll;

protected void OnScroll()
{
   if (this.Scroll != null)
      this.Scroll(this, EventArgs.Empty);
}

protected override void WndProc(ref System.Windows.Forms.Message m)
{
   base.WndProc(ref m);
   if (m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL )
      this.OnScroll();
}

What constant do I need to fire the scroll event for the mouse wheel and keyboard up/down button?

Upvotes: 1

Views: 7544

Answers (2)

AnasBakez
AnasBakez

Reputation: 1230

I have found the other constants for making the scroll on the down arrow key and the end key on the keyboard.this is the code for the CustomListView

using System;
using System.Windows.Forms;

class MyListView : ListView
{
    public event ScrollEventHandler Scroll;
    private const int WM_HSCROLL = 0x114;
    private const int WM_VSCROLL = 0x115;
    private const int MOUSEWHEEL = 0x020A;
    private const int KEYDOWN = 0x0100;

    protected virtual void OnScroll(ScrollEventArgs e)
    {
        ScrollEventHandler handler = this.Scroll;
        if (handler != null) handler(this, e);
    }
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == MOUSEWHEEL || m.Msg == WM_VSCROLL || (m.Msg == KEYDOWN && (m.WParam == (IntPtr)40 || m.WParam == (IntPtr)35)))
        {
            // Trap WM_VSCROLL 
            OnScroll(new ScrollEventArgs((ScrollEventType)(m.WParam.ToInt32() & 0xffff), 0));
        }
    }
}

Upvotes: 1

SwDevMan81
SwDevMan81

Reputation: 49978

The mouse wheel m.Msg value should be:

private const int WM_MOUSEWHEEL = 0x020A;

Upvotes: 5

Related Questions