LauraV
LauraV

Reputation: 61

How to make MouseWheel work on the scrollbar

I am running an application in C# and I need to add Mouse Wheel functionality for a scroll bar.

I set Focus() on some controls from the window where I need that scroll. And still, it doesn't work. But if a minimize the application and maximize again and scroll without any other click it works.

If I click any other control, I cannot use the mouse wheel functionality on the scroll bar. Also, I put Refresh() on some controls.

What can be the problem and what is the solution?

Upvotes: 1

Views: 2604

Answers (2)

user7567645
user7567645

Reputation: 21

It will create scrolling problems, It's not that the scroll position is being reset, but rather that the parent container is scrolling itself to the upper left corner of the user control.

To avoid this, you have to override the ScrollToControl method. Extend System.Windows.Forms.Panel and override the ScrollToControl method there.

Example code:

class CustomScrollBarPanel : System.Windows.Forms.Panel
{
    protected override Point ScrollToControl(Control activeControl)
    {
        return this.AutoScrollPosition;
    }
}

Then use it.

Upvotes: 1

kaka845
kaka845

Reputation: 1

Add a MouseHover event handler and Focus() the scrollbar:

this.panel1.MouseHover += new System.EventHandler(panel1_MouseHover);

private void panel1_MouseHover (object sender, EventArgs e)
{
    this.vScrollBar1.Focus();
}

Upvotes: 0

Related Questions