【发布时间】:2024-01-13 22:36:01
【问题描述】:
我试图捕捉用户在 datagridview 中结束水平滚动的那一刻。我需要这个来重新定位网格标题中的按钮。
到目前为止,我所做的是添加我在此链接上找到的 scrollListener:How can I receive the "scroll box" type scroll events from a DataGridView?
这很好用,只是使用键盘滚动不会触发滚动事件。当我将代码中的鼠标悬停在 Scroll 事件上时,它会显示“当滚动框被鼠标或键盘操作移动时发生”。因此,当使用键盘滚动时应该触发该事件,但它不会。
我的代码是这样的:
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事件? -
您是否需要要求按钮定位仅在用户完成滚动时发生?
-
ValueChanged 事件听起来不错,但是如何使用它来发现用户已停止水平滚动?当用户按住右箭头键并在滚动例如 8 列后放手时,我想做我的代码,而不是 8 次。
标签: c# datagridview