WinForms horizontal scrolling - unexpected behaviour

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luker
    New Member
    • Aug 2009
    • 6

    WinForms horizontal scrolling - unexpected behaviour

    Hi Experts,

    I'm trying to create a panel-derived class with Autoscroll = true that scrolls horizontally when using <Shift>+Mousewh eel. Sounds like a straight forward task, doesn't it?
    I tried this code:
    Code:
    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;
        }
    }
    It does scroll, but not entirely the way it was meant to:
    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?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Scrollbars have properties of "smalljump" and "largejump" ... or ... "smallmove" and "largemove" ... or something like that. Small is for when you click the arror at the end of the slider, large is when you click the slider itself. Commonly you see this a move by line and move by page in some application.

    Try adjusting these values so the jumps are larger. maybe make the small move = to 5 or so, instead of the default of 1.

    Comment

    • luker
      New Member
      • Aug 2009
      • 6

      #3
      Actually, the values are
      HorizontalScrol l.SmallChange = 5 and
      HorizontalScrol l.LargeChange = 996.

      But you have to explain a bit more in-depth how these values work. I just don't get how they could cuase the behaviour I described:

      The mouse wheel doesn't scroll continuously. It's scrolling in steps. I call those steps "mouse wheel ticks". In the application, every mouse wheel tick causes a MouseEventArgs. Delta of 120. This paragraph is nothing to worry about. But the next two are:

      1. Scrolling one mouse wheel tick moves the panel's content. Scrolling another mouse wheel tick moves the scrollbar. Scrolling yet another mouse wheel tick scroll the panel's content again and so forth.
      Scrollbar and content move alternatingly.

      2. Even with constant MouseEventArgs. Deltas of 120 the scrolling distance per mouse wheel tick changes like described.

      Comment

      Working...