I'm trying to create a panel-derived class with Autoscroll = true that scrolls horizontally when using <Shift>+Mousewheel. Sounds like a straight forward task, doesn't it?
I tried this code:
Expand|Select|Wrap|Line Numbers
- protected override void OnMouseWheel(MouseEventArgs e)
- {
- if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
- {
- // Get original horizontal position
- int iHorizontalPosition = HorizontalScroll.Value;
- // Apply change to horizontal position
- iHorizontalPosition += e.Delta;
- // Consider range of HorizontalScroll.Value
- if (iHorizontalPosition < HorizontalScroll.Minimum)
- iHorizontalPosition = HorizontalScroll.Minimum;
- else if (iHorizontalPosition > HorizontalScroll.Maximum)
- iHorizontalPosition = HorizontalScroll.Maximum;
- // Use result to scroll panel
- HorizontalScroll.Value = iHorizontalPosition;
- }
- }
1. When scolling a long way in the same direction, the panel's content scrolls every other mouse wheel tick. The scrollbar moves just on the mouse wheel ticks in between two panel content scrolling mouse wheel ticks.
2. When alternating the scrolling direction with every mouse wheel tick, the scrolling distance grows and shrinks like some sine-wave.
I already had implemented accumulation of consecutive e.Deltas, but that didn't solve the problem.
How is horizontal scrolling of a panel implemented correctly?