I’ll start by what I’m trying to have happen:
I have data loaded in a range, where, say, scrolling all the way to the left puts me on April 1, and scrolling all the way to the right puts me on June 1.
The user positions the scrollbar on April 1st, and clicks the left arrow on the scrollbar. Now the scrollbar is positioned at March 31, and the range of data now spans from March 1-June 1.
Here’s my problem:
I have been handling the left-arrow-click in the Scroll event handler (roughly as follows):
private void horizontalScroll_Scroll(object sender, ScrollEventArgs e) { if (LeftArrowClicked()) { horizontalScroll.Maximum = calculateNewMaximum(earliestDate, latestDate); horizontalScroll.Value = calculateNewPosition(currentDate.AddDays(-1), earliestDate); } }
Stepping through with the debugger, the moment it leaves this event handler, horizontalScroll.Value drops to 0, while horizontalScroll.Maximum stays at the correct value.
I’ll post back later with any clarifications, and answers to questions.
It’s clearly being caused by the
ScrollableControlsetting the value after theScrollevent is fired. You could try extending the control you are using and overriding theOnScrollvirtual method.Edit
You should probably be aware that clicking a scroll bar’s buttons does not raise the
Scrollevent, it only raises theValueChangedevent. (Using the mouse generates both though.)Edit Again
Ah ha! I knew there was a way to do it. What you want to do instead of changing
horizontalScroll.Value, you want to set theNewValueon theScrollEventArgsparameter. This should work: