1

I have a panel in winform. I want to capture both scroll and mouse wheel event for the panel. For both scenario I want to check the scroll bar position.

When scroll bar is at bottom (at the end of scrolling...) the control should fire the event.

I have done this for Panel.Scroll like this:

private void Panel1_Scroll(object sender, ScrollEventArgs e)
{            
    if (e.NewValue == Panel1.VerticalScroll.Maximum - Panel1.VerticalScroll.LargeChange+1)
    {
      //do some operation
    }
}

But for MouseEventArgs there is no value (e.newvalue) to indicate scrollbar position.

How can I get the Scrollbar position from mouse wheel event ?

Also as per my requirement both event call have same logic implementation, so I want to write the logic once.

How can I achieve this ?

Digambar Malla
  • 335
  • 2
  • 4
  • 20
  • You can't of course. The MouseWheel event will get the Scroll event to fire if it actually scrolls the panel so there is no need to help. The panel must contain one or more controls that can get the focus or you must create a [panel control](http://stackoverflow.com/a/3562449/17034) that can get the focus. Beware the flaky behavior of scrollbars due to the thumb size, you must use >= instead of ==. – Hans Passant Dec 16 '15 at 09:30

2 Answers2

1

Well upon further analysis I checked out that panel1.VerticalScroll.Value is equivalent to e.NewValue of ScrollEventArgs.

So for code re-usability below can be used:

private void panel1_Scroll(object sender, ScrollEventArgs e)
{
  panel1_scrollcheck(e.NewValue);
}

private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
  panel1_scrollcheck(panel1.VerticalScroll.Value);
}

private void panel1_scrollcheck(int currPos)
{
  if (currPos == panel1.VerticalScroll.Maximum - panel1.VerticalScroll.LargeChange+1)
    {
      //Put the logic here
    }
}
Digambar Malla
  • 335
  • 2
  • 4
  • 20
  • so you just used my answer and even didn't accept it? – melya Dec 16 '15 at 11:51
  • @meiya, I have accepted ur answer althogh it is partial. And also the if condition is wrong (not sure about ur implementation, but for me the equal condition is working and also the currPos will never be >...if doubt please check the exact question of me...) – Digambar Malla Dec 16 '15 at 12:53
  • Why it is partial? I showed you what to use for solving your problem and give example. Or you was waiting that I will write all code for you? – melya Dec 16 '15 at 12:56
0

Try to you VerticalScroll property of Panel.

    void MouseWheel(object sender, MouseEventArgs e)
    {
        if (_panel.VerticalScroll.Value > _panel.VerticalScroll.Maximum - _panel.VerticalScroll.LargeChange)
            MessageBox.Show("Bottom");
    }
melya
  • 578
  • 5
  • 24
  • Great for what I required. But I am not being able to implement the same logic for scroll and mouse wheel as they are having different eventargs !!! – Digambar Malla Dec 16 '15 at 10:04
  • 1
    But you can use `panel.VerticalScroll.Value` in both cases - for scroll and for mouse wheel instead of `e.NewValue` – melya Dec 16 '15 at 10:06
  • Correct, but scroll and mouse wheel have different eventargs so it can't overload – Digambar Malla Dec 16 '15 at 10:13
  • You can add handler for `Panle.MouseWheel`. `panel.MouseWheel += new MouseEventHandler(panel_MouseWheel);` as I did. I thought you know this. – melya Dec 16 '15 at 10:16
  • Thanks for ur valuable response. I have put the exact solution for what i was looking. Anyway Thanks again :) – Digambar Malla Dec 16 '15 at 11:18