我怎样才能收到“滚动框"?从 DataGridView 输入滚动事件? [英] How can I receive the "scroll box" type scroll events from a DataGridView?
问题描述
我有一个 DataGridView,我正在监听它的 Scroll 事件.这给了我一个 ScrollEventArgs 对象,它的 Type 成员应该告诉我发生的滚动事件的类型.在 MSDN 文档页面 上,它说我应该能够通过侦听 ThumbPosition、ThumbTrack、First、Last 和 EndScroll 类型的事件来检测滚动框的移动.
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.
但是,当我拖动滚动框时,我只能获得 LargeDecrement 和 LargeIncrement 类型的事件.
However, when I drag the scroll box, I only get events of type LargeDecrement and LargeIncrement.
如何访问 ThumbPosition、ThumbTrack、First、Last 和 EndScroll 事件?
How do I get access to the ThumbPosition, ThumbTrack, First, Last and EndScroll events?
推荐答案
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..
}
如您所料,如果您想收听水平滚动事件,请将VerticalScrollBar"更改为HorizontalScrollBar"
As you'd expect, if you want to listen to horizontal scroll events, you change "VerticalScrollBar" to "HorizontalScrollBar"
这篇关于我怎样才能收到“滚动框"?从 DataGridView 输入滚动事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!