如何以编程方式滚动流程图

时间:2014-12-09 11:06:23

标签: .net winforms flowpanel

我正在尝试使用以下代码左右移动面板。

private void btnLeft_Click(object sender, EventArgs e)
{
    if (flowPanelItemCategory.Location.X <= xpos)
    {
        xmin = flowPanelItemCategory.HorizontalScroll.Minimum;
        if (flowPanelItemCategory.Location.X >= xmin)
        {
            xpos -= 100;
            flowPanelItemCategory.Location = new Point(xpos, 0);
        }
    }
}

private void btnRight_Click(object sender, EventArgs e)
{
    if (flowPanelItemCategory.Location.X <= xpos)
    {
        xmax = flowPanelItemCategory.HorizontalScroll.Maximum;
        if (flowPanelItemCategory.Location.X < xmax)
        {
            xpos += 100;
            flowPanelItemCategory.Location = new Point(xpos, 0);
        }
    }
}

但是流程面板的流量不会超过几个像素/点(100),这对应于.HorizontalScroll.Maximum;

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

让我感到惊讶的第一件事就是你设置Location属性的原因。这样你就不会设置滚动位置,但实际上你正在移动FlowLayoutPanel的位置。

您可以试试这个,但只有AutoScroll设置为True才会有效:

private void btnLeft_Click(object sender, EventArgs e)
{
    int scrollValue = flowPanelItemCategory.HorizontalScroll.Value;
    int change = flowPanelItemCategory.HorizontalScroll.SmallChange;
    int newScrollValue = Math.Max(scrollValue - change, flowPanelItemCategory.HorizontalScroll.Minimum);
    flowPanelItemCategory.HorizontalScroll.Value = newScrollValue;
    flowPanelItemCategory.PerformLayout();
}

private void btnRight_Click(object sender, EventArgs e)
{
    int scrollValue = flowPanelItemCategory.HorizontalScroll.Value;
    int change = flowPanelItemCategory.HorizontalScroll.SmallChange;
    int newScrollValue = Math.Min(scrollValue + change, flowPanelItemCategory.HorizontalScroll.Maximum);
    flowPanelItemCategory.HorizontalScroll.Value = newScrollValue;
    flowPanelItemCategory.PerformLayout();
}

此代码获取当前滚动视图,并根据“scroll-step-size”递增或递减它,但永远不会超出它的边界。