Tharif
Tharif

Reputation: 13971

Event getting triggered multiple times on scroll event handler

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

Upvotes: 3

Views: 2336

Answers (1)

CharithJ
CharithJ

Reputation: 47540

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.

Upvotes: 4

Related Questions