How to detect if WinForms Panel has scrolled to th

2019-07-29 15:55发布

问题:

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?

回答1:

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

  • ScrollableControl.Scroll
  • ScrollEventHandler
  • ScrollEventArgs

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.



回答2:

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.



回答3:

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;
}


回答4:

if(topicContainer.VerticalScroll.Value == topicContainer.VericalScroll.Maximum)
{


}