Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a winform application where I want a event to be fired when the scrollbar reaches the bottom of panel.

I tried this:
C#
private void Panel1_Scroll(object sender, ScrollEventArgs e)
{
    //some operation
}

But it is firing event everytime I scroll the Scrollbar.

How to achieve this ?
Posted
Comments
Stephen Hewison 15-Dec-15 5:57am    
There is no scroll at bottom event so you've have to use the event you've identified. Then test the scroll position to see if it's at the bottom.

1 solution

For whatever reasons Microsoft made detecting scrolling to bottom, and to right, in Windows Forms, much more tricky than was necessary.

The Panel has serious flaws in its scrolling behavior: the ScrollEventArgs.Type field which should hold an enumeration value of Type ScrollEventArgs is (according to Kornfeld Peter's recent source code examination) not really implemented.
C#
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
    VScrollProperties vsp = panel1.VerticalScroll;

    int scrollmax = vsp.Maximum - vsp.LargeChange + 1;

    if (e.NewValue == scrollmax) scrolledToBottom(e.NewValue, scrollmax);
}

// test
private void scrolledToBottom(int nv, int smax)
{
    Console.WriteLine("scrolled to bottom: {0} {1}", nv, smax);
}
Note:

You could create variables at Form level, to hold the values of the Panel's VerticalScroll.Maximum and VerticalScroll.LargeChange, then initialize them in the Form Load Event, or the constructor of the Form if you don't manipulate those properties in your code ... or, if you do manipulate them, you'll need to update the variables.
 
Share this answer
 
v4
Comments
CHill60 15-Dec-15 6:24am    
Beat me to it - 5'd
Kornfeld Eliyahu Peter 15-Dec-15 7:16am    
Just went to see the source code for ScrollBar - there is no way the control itself (you can send a WM to it and it will propagate it) fire an event with Last or First. Just no code for that...It seems worst than you described it...
Digambar Malla 15-Dec-15 8:10am    
This helped me for what I was looking for.

if (e.NewValue == panel1.VerticalScroll.Maximum - panel1.VerticalScroll.LargeChange + 1)
{

if(e.NewValue!=e.OldValue)// checking when the scrollbar is at bottom and user clicks/scrolls the scrollbar again to bottom

{
MessageBox.Show("Test"); //Or some operation
}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900