如何在动态增长时设置ScrollBar的值?

时间:2022-04-29 19:17:53

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.

我在一个范围内加载了数据,例如,一直向左滚动让我在4月1日,向右滚动让我在6月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.

用户在4月1日定位滚动条,然后单击滚动条上的向左箭头。现在滚动条定位于3月31日,现在数据范围从3月1日到6月1日。

Here's my problem:

这是我的问题:

I have been handling the left-arrow-click in the Scroll event handler (roughly as follows):

我一直在处理Scroll事件处理程序中的左箭头单击(大致如下):

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.

单步调试器,当它离开此事件处理程序时,horizo​​ntalScroll.Value将降为0,而horizo​​ntalScroll.Maximum将保持正确的值。

I'll post back later with any clarifications, and answers to questions.

我会稍后回复任何澄清,并回答问题。

2 个解决方案

#1


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.

这显然是由ScrollableControl设置Scroll事件触发后的值引起的。您可以尝试扩展正在使用的控件并覆盖OnScroll虚拟方法。

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.)

您应该知道单击滚动条的按钮不会引发Scroll事件,它只会引发ValueChanged事件。 (尽管使用鼠标生成两者。)


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:

啊哈!我知道有办法做到这一点。你想要做什么而不是改变horizo​​ntalScroll.Value,你想在ScrollEventArgs参数上设置NewValue。这应该工作:

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

#2


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

尝试设置e.NewValue而不是horizo​​ntalScroll.Value。然后,控件在执行自己的逻辑时会遵循该值。

#1


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.

这显然是由ScrollableControl设置Scroll事件触发后的值引起的。您可以尝试扩展正在使用的控件并覆盖OnScroll虚拟方法。

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.)

您应该知道单击滚动条的按钮不会引发Scroll事件,它只会引发ValueChanged事件。 (尽管使用鼠标生成两者。)


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:

啊哈!我知道有办法做到这一点。你想要做什么而不是改变horizo​​ntalScroll.Value,你想在ScrollEventArgs参数上设置NewValue。这应该工作:

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

#2


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

尝试设置e.NewValue而不是horizo​​ntalScroll.Value。然后,控件在执行自己的逻辑时会遵循该值。

相关文章