我想捕获用户在datagridview中结束水平滚动的时刻。我需要这个来重新定位网格标题中的按钮。datagridview使用键盘滚动时滚动事件不会触发
我迄今所做的是增加一个scrollListener我这个链接上找到:How can I receive the "scroll box" type scroll events from a DataGridView?
这个作品非常好,exept使用的键盘,滚动不火的滚动事件。当我在滚动事件中将鼠标放在代码中时,它会声明“在鼠标或键盘操作移动滚动框时发生”。所以事件应该在用键盘滚动时触发,但事实并非如此。
我的代码是这样的:
bool addScrollListener(DataGridView dgv)
{
// capture horizonal scrolling and redirect to s_Scroll. Purpose is to redraw buttons after scrolling
bool Result = false;
Type t = dgv.GetType();
PropertyInfo pi = t.GetProperty("HorizontalScrollBar", 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);
Result = true;
}
return Result;
}
void s_Scroll(object sender, ScrollEventArgs e)
{
// if grid is done scrolling horizontal, than redraw our buttons
if (e.Type == ScrollEventType.EndScroll)
{
// code works well, but only get here when scrolling with mouse
PositionButtons();
}
}
所以我的问题是,当使用鼠标s_Scroll事件被激发用户滚动,但使用s_Scroll事件不会触发在所有的键盘滚动时。
我的问题是我该如何解决这个问题,以便在两种情况下触发事件, ,如果不可行,还有另一种方法可以从datagridview捕获水平滚动的结尾。
您是否尝试使用'ValueChanged'事件而不是'Scroll'事件? – Bioukh 2015-04-02 10:20:41
您是否需要仅在用户完成滚动时发生按钮定位? – 2015-04-02 10:20:44
ValueChanged事件听起来不错,但我怎么用它来发现用户已经停止了水平滚动?当用户按住右箭头键并且在滚动后例如8列时,比我想要做我的代码,而不是8次。 – GuidoG 2015-04-02 10:52:46