How to detect if WinForms Panel has scrolled to th

2019-07-29 15:41发布

I am developing a WinForms application in which there is a panel which contains some user-controls. When the panel loads for the first time it shows 10 user controls. But when it is scrolled down completely it should load and append more user controls at the end of the panel. I am trying to achieve this using this code:

private void topicContainer_Scroll(object sender, ScrollEventArgs e)
{
      if (e.NewValue== topicContainer.VerticalScroll.Value)
                MessageBox.Show("Topics load here");
}

Its just a trial. I don't know what actually this NewValue means. So, can you please tell how to accomplish my this task?

4条回答
Evening l夕情丶
2楼-- · 2019-07-29 15:57

MSDN covers the case quite nicely. Have you checked it out?

Remember though about the weird behavior of scrollbars: user is never able to reach its Maximum value. Read remarks in the ScrollBar.Maximum MSDN help page.

查看更多
Lonely孤独者°
3楼-- · 2019-07-29 16:11
if(topicContainer.VerticalScroll.Value == topicContainer.VericalScroll.Maximum)
{


}
查看更多
对你真心纯属浪费
4楼-- · 2019-07-29 16:13

As others have mentioned, the scrollbar never does reach its Maximum value, and that is due to the LargeChange property getting factored into the equation:

private void topicContainer_Scroll(object sender, ScrollEventArgs e)
{
  VScrollProperties vs = topicContainer.VerticalScroll;
  if (e.NewValue == vs.Maximum - vs.LargeChange + 1) {
    // scrolled to the bottom
  }
}

The + 1 is for the zero-based offset. If you set the AutoScrollMinSize height property to 500, the Maximum value is actually 499.

查看更多
太酷不给撩
5楼-- · 2019-07-29 16:18

This function must be placed in a static class.

public static bool IsScrolledDown(this ScrollableControl c) {
    return !c.VerticalScroll.Visible || c.VerticalScroll.Value == c.VerticalScroll.Maximum - c.VerticalScroll.LargeChange + 1;
}
查看更多
登录 后发表回答