我正在尝试使用以下代码左右移动面板。
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;
我该如何解决这个问题?
答案 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”递增或递减它,但永远不会超出它的边界。