Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling scroll event on listview in c#

I have a listview that generates thumbnail using a backgroundworker. When the listview is being scrolled i want to pause the backgroundworker and get the current value of the scrolled area, when the user stopped scrolling the listview, resume the backgroundworker starting from the item according to the value of the scrolled area.

Is it possible to handle scroll event of a listview? if yes how? if not then what is a good alternative according to what i described above?

like image 979
murasaki5 Avatar asked Dec 05 '09 08:12

murasaki5


3 Answers

You'll have to add support to the ListView class so you can be notified about scroll events. Add a new class to your project and paste the code below. Compile. Drop the new listview control from the top of the toolbox onto your form. Implement a handler for the new Scroll event.

using System;
using System.Windows.Forms;

    class MyListView : ListView {
      public event ScrollEventHandler Scroll;
      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 == 0x115) { // Trap WM_VSCROLL
          OnScroll(new ScrollEventArgs((ScrollEventType)(m.WParam.ToInt32() & 0xffff), 0));
        }
      }
    }

Beware that the scroll position (ScrollEventArgs.NewValue) isn't meaningful, it depends on the number of items in the ListView. I forced it to 0. Following your requirements, you want to watch for the ScrollEventType.EndScroll notification to know when the user stopped scrolling. Anything else helps you detect that the user started scrolling. For example:

ScrollEventType mLastScroll = ScrollEventType.EndScroll;

private void myListView1_Scroll(object sender, ScrollEventArgs e) {
  if (e.Type == ScrollEventType.EndScroll) scrollEnded();
  else if (mLastScroll == ScrollEventType.EndScroll) scrollStarted();
  mLastScroll = e.Type;
}
like image 74
Hans Passant Avatar answered Oct 19 '22 19:10

Hans Passant


Based upon the post that @Adriaan Stander posted my class for raising scroll events is below.

internal class ControlScrollListener : NativeWindow, IDisposable
{
    public event ControlScrolledEventHandler ControlScrolled;
    public delegate void ControlScrolledEventHandler(object sender, EventArgs e);

    private const uint WM_HSCROLL = 0x114;
    private const uint WM_VSCROLL = 0x115;
    private readonly Control _control;

    public ControlScrollListener(Control control)
    {
        _control = control;
        AssignHandle(control.Handle);
    }

    protected bool Disposed { get; set; }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (Disposed) return;

        if (disposing)
        {
            // Free other managed objects that implement IDisposable only
        }

        // release any unmanaged objects
        // set the object references to null
        ReleaseHandle();

        Disposed = true;
    }

    protected override void WndProc(ref Message m)
    {
        HandleControlScrollMessages(m);
        base.WndProc(ref m);
    }

    private void HandleControlScrollMessages(Message m)
    {
        if (m.Msg == WM_HSCROLL | m.Msg == WM_VSCROLL)
        {
            if (ControlScrolled != null)
            {
                ControlScrolled(_control, new EventArgs());
            }
        }
    }
}

Use it like so...

Declare a field:

 private ControlScrollListener _processListViewScrollListener;

Instantiate it with the controls which you need to know issrolling:

_processListViewScrollListener = new ControlScrollListener(ProcessesListView);

Wire in a handler:

_processListViewScrollListener.ControlScrolled += ProcessListViewScrollListener_ControlScrolled;

Handler the event:

void ProcessListViewScrollListener_ControlScrolled(object sender, EventArgs e)
{
    // do what you need to do
}

The event args in the event raised could be tweaked to contain more useful information. I just needed to know my control had been scrolled!

like image 38
Dib Avatar answered Oct 19 '22 20:10

Dib


See this post ListView Scroll Event

Use the native window class to listen for the scroll messages on the listbox. Will work with any control.

like image 1
Adriaan Stander Avatar answered Oct 19 '22 21:10

Adriaan Stander