如何以编程方式滚动面板

我有一个带有一些内容的System.Windows.Forms.Panel

我试图以编程方式向上或向下滚动面板(垂直)。

我已经尝试将AutoScrollPosition属性设置为面板上的新Point ,但似乎没有这样做。

我将AutoScroll属性设置为true。

我甚至试图按照此处的建议设置VerticalScroll.Value两次,但这似乎也不起作用。

这就是我目前正在做的事情:

 //I have tried passing both positive and negative values. panel.AutoScrollPosition = new Point(5, 10); 

AutoScrollPosition上的X和Y值保持为0和0。

任何帮助或方向将非常感谢它。

提前致谢,

马尔万

这是一个解决方案。 我想你可以使用Win32以任意位置滚动你的Panel ,但是有一个简单的技巧可以帮助你在这里达到你的要求:

 public void ScrollToBottom(Panel p){ using (Control c = new Control() { Parent = p, Dock = DockStyle.Bottom }) { p.ScrollControlIntoView(c); c.Parent = null; } } //use the code ScrollToBottom(yourPanel); 

或者使用扩展方法以方便:

 public static class PanelExtension { public static void ScrollToBottom(this Panel p){ using (Control c = new Control() { Parent = p, Dock = DockStyle.Bottom }) { p.ScrollControlIntoView(c); c.Parent = null; } } } //Use the code yourPanel.ScrollToBottom(); 

UPDATE

如果你想设置确切的位置,修改上面的代码可以帮助:

 //This can help you control the scrollbar with scrolling up and down. //The position is a little special. //Position for scrolling up should be negative. //Position for scrolling down should be positive public static class PanelExtension { public static void ScrollDown(this Panel p, int pos) { //pos passed in should be positive using (Control c = new Control() { Parent = p, Height = 1, Top = p.ClientSize.Height + pos }) { p.ScrollControlIntoView(c); } } public static void ScrollUp(this Panel p, int pos) { //pos passed in should be negative using (Control c = new Control() { Parent = p, Height = 1, Top = pos}) { p.ScrollControlIntoView(c); } } } //use the code, suppose you have 2 buttons, up and down to control the scrollbar instead of clicking directly on the scrollbar arrows. int i = 0; private void buttonUp_Click(object sender, EventArgs e) { if (i >= 0) i = -1; yourPanel.ScrollUp(i--); } private void buttonDown_Click(object sender, EventArgs e) { if (i < 0) i = 0; yourPanel.ScrollDown(i++); } 

您可能想要使用的另一种解决方案是使用Panel.VerticalScroll.Value 。 但是我认为你需要更多的研究才能使它按预期工作。 因为我可以看到一旦改变Value ,滚动条位置和控制位置就不能很好地同步。 请注意, Panel.VerticalScroll.Value应介于Panel.VerticalScroll.MinimumPanel.VerticalScroll.Maximum之间。

试试这个: – panel.ScrollControlIntoView(childcontrol);

这应该工作。 childcontrol是您要在显示区域中显示的特定控件。

如果您有一个派生自Panel的类,则调用这两个受保护的方法来滚动面板:

 // The bottom is off screen; scroll down. These coordinates must be negative or zero. SetDisplayRectLocation(0, AutoScrollPosition.Y - item.BoundingRect.Bottom + ClientRectangle.Bottom); AdjustFormScrollbars(true); 

在我的例子中, item.BoundingRect.Bottom是缩略图底部的Y坐标,我需要向下滚动面板,以便整个缩略图可见。

@King King创建临时控件的解决方案只是为了让滚动可以完成对我来说似乎“沉重”。 @Hans Passant建议设置AutoScrollMinSizeAutoScrollPosition对我不起作用。

AutoScroll保留为其默认值“true”。

使用@King King已回答代码,如果要隐藏水平和垂直滚动条,只需在构造函数或初始化中应用以下代码即可。

  yourPanel.AutoScroll = false; yourPanel.HorizontalScroll.Maximum = 0; yourPanel.HorizontalScroll.Visible = false; yourPanel.VerticalScroll.Maximum = 0; yourPanel.VerticalScroll.Visible = false; yourPanel.AutoScroll = true; 

我有一个问题,我不能让我的面板滚动回顶部。 我尝试了很多东西,让面板在用许多控件填充之后滚动回顶部。

无论我做了什么,总是将VScroll栏放到底部。

经过详尽的测试后,我发现这是因为我的控件将TabStop属性设置为true(默认情况下是用户控件)导致了问题。

将TabStop设置为false修复它。

这令人惊讶的工作! 注意代码中的MINUS SIGN。 设置滚动位置时有奇怪的行为。 如果将位置设置为精确值(50),则下次读取时会变为负值(-50)。 所以你必须在设置新的滚动值之前将其反转。

向下滚动:

 private void ButtonScrollDown_OnClick(object sender, EventArgs e) { Point current = yourScrollPanel.AutoScrollPosition; Point scrolled = new Point(current.X, -current.Y + 10); yourScrollPanel.AutoScrollPosition = scrolled; } 

同样向上滚动,( – 当前.Y – 10)