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

umu*_*eme 2 c# winforms

flowlayoutpanel在winform中有动态添加图像.我希望vertical scroll bar始终位于底部,显示最后添加的图像.我怎样才能做到这一点?我有

AutoScroll = true

FLow Direction = Top Down

Wrap Content = False

Han*_*ant 8

可滚动容器控件(如FlowLayoutPanel)会自动保持控件的焦点在视图中.但PictureBox很特别,它无法获得焦点.因此,您必须通过明确要求FLP使添加的控件可见来提供帮助,使用其ScrollControlIntoView()方法.像这样:

    var pic = new PictureBox();
    //...
    flowLayoutPanel1.Controls.Add(pic);
    flowLayoutPanel1.ScrollControlIntoView(pic);
Run Code Online (Sandbox Code Playgroud)

具有强大的优势,适用于您应用于FLP的任何布局设置.您还可以修改AutoScrollPosition属性,但更难做到这一点:

    flowLayoutPanel1.AutoScrollPosition = new Point(
        pic.Right  - flowLayoutPanel1.AutoScrollPosition.X, 
        pic.Bottom - flowLayoutPanel1.AutoScrollPosition.Y);
Run Code Online (Sandbox Code Playgroud)