始终滚动到垂直滚动条底部

5
我在我的WinForm中有一个FlowLayoutPanel,其中的图像是动态添加的。我希望竖直滚动条始终位于底部,显示最后添加的图像。我应该如何做呢?
我已经设置了以下属性:
AutoScroll = true Flow Direction = Top Down
Wrap Content = False
5个回答

18

可滚动的容器控件,比如FlowLayoutPanel,会自动将拥有焦点的控件保持在视图中。但是PictureBox是特殊的,它无法接收焦点。因此,您需要通过显式要求FLP使添加的控件可见,使用其ScrollControlIntoView()方法来进行帮助。像这样:

    var pic = new PictureBox();
    //...
    flowLayoutPanel1.Controls.Add(pic);
    flowLayoutPanel1.ScrollControlIntoView(pic);

这种方法的强大之处在于它适用于您应用于FLP的任何布局设置。您还可以尝试调整AutoScrollPosition属性,但这更难正确地完成:

    flowLayoutPanel1.AutoScrollPosition = new Point(
        pic.Right  - flowLayoutPanel1.AutoScrollPosition.X, 
        pic.Bottom - flowLayoutPanel1.AutoScrollPosition.Y);

谢谢。它起作用了。我可以使用两种方法,因为我在PictureBox下面有LinkLabel。 - umuieme

1
这是一种将最后一个控件强制显示的方法。
        flowLayoutPanel.ScrollControlIntoView(Control_To_Add); // Control_To_Add is the control we want to scroll to
        Button TempButton = new Button();
        TempButton.Width = _Panel.ClientRectangle.Width - 6; // Make the last control in the _Panel
        flowLayoutPanel.Controls.Add(TempButton); // We add this TempButton so we can scroll to the bottom of the _Panel.Controls
        flowLayoutPanel.ScrollControlIntoView(b); // We scroll to TempButton at the bottom of the _Panel.Controls
        flowLayoutPanel.Controls.Remove(b); // We remove TempButton
        b.Dispose(); // clean up

1

试试这个:

scrollBar.Value=scrollBar.Maximum;

这里的scrollBar是您在winform中的ScrollBar控件。

更多详情,请查看此处。


0

强制 FlowLayoutPanel 滚动并显示所有控件。

代码更正:

flowLayoutPanel.ScrollControlIntoView(Control_To_Add); // Control_To_Add is the control we want to scroll to
Button TempButton = new Button();
TempButton.Width = _Panel.ClientRectangle.Width - 6; // Make the last control in the _Panel
flowLayoutPanel.Controls.Add(TempButton); // We add this TempButton so we can scroll to the bottom of the _Panel.Controls
flowLayoutPanel.ScrollControlIntoView(TempButton); // We scroll to TempButton at the bottom of the _Panel.Controls
flowLayoutPanel.Controls.Remove(TempButton); // We remove TempButton
b.Dispose(); // clean up

b.Dispose(); 应该改为 TempButton.Dispose()。 - bclough
使用滚动事件(Scroll event)可以实现每次滚动时发生的效果。然而,这里呈现的代码可以在任何时候调用。 - bclough

0

这是正确的方式:

MyControl uct = new MyControl();
uct.Parent = flowLayoutPanel;

this.ActiveControl = uct;



if (flowLayoutPanel.VerticalScroll.Visible)
{
    flowLayoutPanel.ScrollControlIntoView(uct);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接