tags:

views:

351

answers:

2

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.

+1  A: 

It's clearly being caused by the ScrollableControl setting the value after the Scroll event is fired. You could try extending the control you are using and overriding the OnScroll virtual method.

protected override void OnScroll(ScrollEventArgs se)
{
  base.OnScroll(se);

  // Do stuff now
}


Edit

You should probably be aware that clicking a scroll bar's buttons does not raise the Scroll event, it only raises the ValueChanged event. (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 the NewValue on the ScrollEventArgs parameter. This should work:

private void horizontalScroll_Scroll(object sender, ScrollEventArgs e)
{
    if (LeftArrowClicked())
    {
        horizontalScroll.Maximum = calculateNewMaximum(earliestDate, latestDate);
        e.NewValue = calculateNewPosition(currentDate.AddDays(-1), earliestDate);
    }
}
Samuel
+2  A: 

Try setting e.NewValue instead of horizontalScroll.Value. The control will then respect this value when executing its own logic.

kvb