3

While I was adding code to scroll event of a panel in c#, I found a strange behavior.

I have added a panel and inside the panel (auto-Scroll = true) there is a groupbox.

As shown below clicking the scroll moves scroll bar to a small distance.

enter image description here

At the same time, when I add a message box in the event to display a notification that a scroll has taken place, multiple message box are popping out.

enter image description here

Why is that?

I have already planned to add some logic when scrolling, but if it occurs multiple times then how it could be possible?

Here is the event handler:

private void panel1_Scroll_1(object sender, ScrollEventArgs e)
{
    MessageBox.Show("ScrollBar is clicked");
}
Brendan Green
  • 11,676
  • 5
  • 44
  • 76
Tharif
  • 13,794
  • 9
  • 55
  • 77
  • http://stackoverflow.com/questions/9220691/track-bar-only-fire-event-on-final-value-not-ever-time-value-changes – CharithJ Jul 29 '15 at 04:49
  • but that is explained for a trackbar.. – Tharif Jul 29 '15 at 04:50
  • Similar handling is required. You cannot stop it firing many times. You should handle it appropriately in a similar way that is explained in that thread. – CharithJ Jul 29 '15 at 04:51
  • thank you @CharithJ,but one more question..then why does the pic1 showed like that ? – Tharif Jul 29 '15 at 04:53
  • Because your event has been executed multiple times while scroll bar poistion is being changed. – CharithJ Jul 29 '15 at 05:03
  • but the movement of scroll was limited in first pic ?..If we add some event only it triggers multiple time ? correct me if im wrong.. – Tharif Jul 29 '15 at 05:05

1 Answers1

3

This is just how scroll event works, it fires many times while the panel is scrolling.

Try ScrollEventArgs.Type EndScroll which should be the last scroll event.

  private void panel1_Scroll_1(object sender, ScrollEventArgs e)
    {
        if (e.Type == ScrollEventType.EndScroll)
            MessageBox.Show("ScrollBar is clicked");
    }

If above doesn't help for your case you will need to handle those multiple events by using one of approaches explains in this thread.

Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131