繁体   English   中英

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

[英]Always Scroll to bottom in vertical scroll bar

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

AutoScroll = true

FLow Direction = Top Down

Wrap Content = False

可滚动容器控件(如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);

尝试这个:

scrollBar.Value=scrollBar.Maximum;

这里scrollBar是winform中的ScrollBar控件。

有关详细信息, 请检查此项。

这是一种强制最后一个控件进入视图的方法。

        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

强制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

这是正确的方法:

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

this.ActiveControl = uct;



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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM