Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I receive the "scroll box" type scroll events from a DataGridView?

I have a DataGridView, and I'm listening to its Scroll event. This gives me a ScrollEventArgs object whose Type member is supposed to tell me the type of scroll event that has occurred. On the MSDN documentation page it says I should be able to detect movement of the scroll box by listening for events with types ThumbPosition, ThumbTrack, First, Last and EndScroll.

However, when I drag the scroll box, I only get events of type LargeDecrement and LargeIncrement.

How do I get access to the ThumbPosition, ThumbTrack, First, Last and EndScroll events?

like image 436
Simon Avatar asked Jan 23 '09 09:01

Simon


People also ask

What is scroll event throttle?

Throttle is normally used when you have a function that is called continuously while the user is interacting with your page, e.g. while scrolling. Debounce is used to call a function when the user has stopped interacting, e.g. when they have stopped typing in an input field.


1 Answers

using System.Reflection;
using System.Windows.Forms;

bool addScrollListener(DataGridView dgv)
{
    bool ret = false;

    Type t = dgv.GetType();
    PropertyInfo pi = t.GetProperty("VerticalScrollBar", BindingFlags.Instance | BindingFlags.NonPublic);
    ScrollBar s = null;

    if (pi != null)
        s = pi.GetValue(dgv, null) as ScrollBar;

    if (s != null)
    {
        s.Scroll += new ScrollEventHandler(s_Scroll);
        ret = true;
    }

    return ret;
}

void s_Scroll(object sender, ScrollEventArgs e)
{
    // Hander goes here..
}

As you'd expect, if you want to listen to horizontal scroll events, you change "VerticalScrollBar" to "HorizontalScrollBar"

like image 67
Simon Avatar answered Sep 20 '22 10:09

Simon