Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...